Problems receiving data over a TCP client socket

I'm trying to make a TCP Client program in C where the client will start up, connect to a server. Then it will send a little information and then just listen to what it receives and react accordingly.

The part that I'm having trouble with is the continuous listening. Here is what I have

...

while (1) {
   numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0);
   buf[numbytes] = '\0';
   printf("Received: %s\n", buf);
   // more code to react goes here
}

...

Upon connecting to the server, after sending two lines of data, the server should receive a good bit of information, but when I run this, it prints:

Received:

And then continues to just sit there until i force it to close.

** EDIT ** when i do what Jonathan told me to do, I get the following:

Count: -1, Error: 111, Received:

So that means its erroring, but what do i do about it?

Answers


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);
    buf[numbytes] = '\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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>

You should see something similar.


I strongly recommend Beej's Guide to Network Programming.

This section in particular has code for a client which does exactly what you ask.


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);

What's with the infinite loop? Why not use select() so that you only call recv() when there's actual data to be read?

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 connections[MAX_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 < MAX_CONNECTIONS; i++ )
    {
      if( connections[i] > 0 )
      {
        FD_SET(&in_fd, connections[i]);
        FD_SET(&out_fd, connections[i]);
        FD_SET(&exc_fd, connections[i]);
      }
    }
    select(&in_fd, &out_fd, &exc_fd, NULL); // this will block until there's an I/O to handle.
    for( i = 0; i < MAX_CONNECTIONS; i++ )
    {
      if( FD_ISSET(&exc_fd, connections[i]) )
      { /* error occurred on this connection; clean up and set connection[i] to 0 */ }
      else
      {
        if( FD_ISSET(&in_fd, connections[i]) )
        { /* handle input */ }
        if( FD_ISSET(&out_fd, connections[i]) )
        { /* handle output */ }
      }
    }
  }
}

Need Your Help

Aggregate function in Kohana Jelly ORM

php kohana jelly

could you help me to find out how to insert aggregate function using Kohana - Jelly module?

Pocket authentication returns BadRequest with content-type 'application/json'

json node.js http-headers pocket

I am retrieving access token for pocket api. I am able to do so successfully using a Http POST request with content type as application/x-www-form-urlencoded.