Problems receiving data over a TCP client socket in 'C?

Print out the number of bytes received - it is likely to be zero, but confirm that.

Print out the number of bytes received - it is likely to be zero, but confirm that. It would be worth checking that you aren't getting an error - and therefore underflowing your buffer. Note: from here onwards is the work of Pax - thank you, and I've converted it to Community Wiki so I don't get rep points undeservedly.

The following code will do this. Try it and report back on the results, please. While (1) { numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0); bufnumbytes = '\0'; printf("Count: %d, Error: %d, Received: %s\n", numbytes, errno, buf); // more code to react goes here } After question edit: Error number 111 is ECONNREFUSED - this is not a usual error code for recv(), but is more suited to the open-type call (open(), connect(), etc).

In any case, ECONNREFUSED is a problem at the server end, not the client - the server has purposefully refused to accept your incoming connection, so you will need to investigate that end of the link. In order to test this, change your code so that it's connecting to www.microsoft.com on port 80, then send a couple of lines of any old rubbish. You should get back an error from their web server indicating a malformed HTTP request.

This will prove there's no problem on your client end. This is what I get back when I telnet www.microsoft.com 80 and type in hello followed by ENTER twice: HTTP/1.1 400 Bad Request Content-Type: text/html; charset=us-ascii Server: Microsoft-HTTPAPI/2.0 Date: Thu, 27 Nov 2008 01:45:09 GMT Connection: close Content-Length: 326 Bad Request Bad Request - Invalid Verb HTTP Error 400. The request verb is invalid.

You should see something similar.

When recv returns 0 it indicates the socket was closed by the remote host, so it probably isn't 0. It might be -1, indicating an error, and the error might be EWOULDBLOCK or EAGAIN indicate the non-blocking read failed because there's no data to receive. – SoapBox Nov 26 '08 at 2:45 You're welcome, @JL, I'd rather modify a half-decent answer than try to pinch rep, especially when your response was correct - all I did after that was flesh out some options for the questioner to investigate.

– paxdiablo Nov 27 '08 at 12:08.

I strongly recommend Beej's Guide to Network Programming. This section in particular has code for a client which does exactly what you ask.

This is where I got the code that I have, and as far as I can tell, it does not actually loop the receiving action. Just receives once. – The.Anti.

9 Nov 26 '08 at 3:01.

EDIT: the answer below was based on a misunderstanding of the question - the OP's code is actually trying to recv() on a socket that it open()ed to a remote server. Stuff below left for posterity. Please show more of your code.

A few observations though: is the socket listen()ing? Have you accept()ed the incoming connection it's slightly unusual to use recv() on a TCP socket. Use read() instead.

Use strerror() to convert the 111 error code into the local error string - each UNIX O/S can have its own mapping from numbers to Exxx error codes so we can't tell what this error is on your system. The normal code loop (for a single threaded non-forking app) looks like: s = socket(); err = listen(s, n); // n = backlog number while (1) { int fd = accept(s, &addr, sizeof(addr)); while (1) { int numrecv = read(fd, ...); // break if eof } close(fd); } close(s).

– Graeme Perrow Nov 26 '08 at 14:30 ok, slightly unusual. Unless you need socket specific flags I prefer to use a normal 'read' function so that any type of file descriptor can be used. – Alnitak Nov 26 '08 at 14:39 I'm not trying to Listen for incoming connections.

This is a client. – The.Anti. 9 Nov 26 '08 at 14:45 ah - ok - misunderstood the messsage flow.In which case your 111 is probably ECONNREFUSED, meaning that the other end didn't accept the connection.

Check the result of your connect() system call. – Alnitak Nov 26 '08 at 14:47.

In a former life I wrote networking code for a MUD, and I can still write the polling loop in my head. The loop in ROM 2.3 went something like this (from memory, so forgive me if the macro parameters are in the wrong order): #define MAX_CONNECTIONS 256 int main(int argc, char *argv) { int I = 0; int running = 1; int connectionsMAX_CONNECTIONS; while( running ) { fd_set in_fd, out_fd, exc_fd; FD_ZERO(in_fd); FD_ZERO(out_fd); FD_ZERO(exc_fd); for( I = 0; I 0 ) { FD_SET(&in_fd, connectionsi); FD_SET(&out_fd, connectionsi); FD_SET(&exc_fd, connectionsi); } } select(&in_fd, &out_fd, &exc_fd, NULL); // this will block until there's an I/O to handle. For( I = 0; I.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions