#define PROXY_PORT 8080 #define MAXLENGTH 10000 using namespace std; int sock_read(int sockfd, char *buf, const size_t count); int sock_write(int sockfd, char *buf, const size_t count); int main ( ) { int sock = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in s_addr; memset(&s_addr, 0, sizeof(s_addr)); s_addr.sin_port = htons(PROXY_PORT); s_addr.sin_family = AF_INET; cout << "Bind: " << bind(sock, (struct sockaddr*)&s_addr, sizeof(s_addr)) << endl; cout << "Listen: " << listen(sock, 5) << endl; while (1) { // listening permanently struct sockaddr_in peer; memset(&peer, 0, sizeof(peer)); socklen_t slen = sizeof(peer); int new_s = accept(sock, (struct sockaddr*)&peer, &slen); //accept connections char buf[MAXLENGTH]; int size = sock_read(new_s,buf,sizeof(buf)); //read message //--------------------------------------------------------------------------// // ... more code } //read data on socket int sock_read(int sockfd, char *buf, const size_t count) { size_t bytes_read = 0; int this_read; while (bytes_read < count) { do { this_read = read(sockfd, buf, count-bytes_read); } while (this_read < 0 && errno == EINTR); if (this_read < 0) { return this_read; } else if (this_read == 0) { return bytes_read; } bytes_read += this_read; buf += this_read; } return bytes_read; }