<p>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.</p>

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

<pre><code>...

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

...
</code></pre>

<p>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:</p>

<blockquote>
  <p>Received:</p>
</blockquote>

<p>And then continues to just sit there until i force it to close.</p>

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

<blockquote>
  <p>Count: -1, Error: 111, Received:</p>
</blockquote>

<p>So that means its erroring, but what do i do about it?</p>

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.