Print the number of bytes received - it will probably be zero, but confirm this.
It would be nice to verify that you are not receiving an error message - and therefore overflow your buffer.
[Note: from here on, Pax's work - thanks , and I converted it to the Community Wiki, so I don't get rep rep undeservedly.]
The following code will do this. Try and report 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 editing the question:
Error number 111 ECONNREFUSED is not an ordinary error code for recv (), but is more suitable for calling an open type (open (), connect (), etc.).
In any case, ECONNREFUSED is a problem on the server, not the client - the server deliberately refused to accept your incoming connection, so you will need to examine this end of the link.
To verify this, change your code so that it connects to www.microsoft.com on port 80 and then sends a couple lines of any old garbage. You must return an error from your web server by specifying an invalid HTTP request. This will not be a problem for your client.
This is what I will return when I telnet www.microsoft.com 80 and type hello and then 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 like this.