reading http message on a socket


K

kant

Guys,
i'm trying to receive a http message: i open a socket, bind and listen ..
then accept (and it works so far); when i receive to read the data on
the socket with the sock_read function, everything gets frozen

Any help from a more experienced programmer would be so appreciated.


#define PROXY_PORT 8080
#define MAXLENGTH 30000
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

}

// here follows the sock_read function
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;

}
 
Ad

Advertisements

V

Victor Bazarov

kant said:
i'm trying to receive a http message: i open a socket, bind and
listen ..
then accept (and it works so far); when i receive to read the data on
the socket with the sock_read function, everything gets frozen

Any help from a more experienced programmer would be so appreciated.
[..]

Since reading sockets is not defined in the language itself or in
the standard library, you should consider asking in the newsgroup
dedicated to your platform.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top