Question

I'm using the TCPClient code from this site in order to make a basic browsing application in C. When I type GET /index.html as the message, it returns this:

HTTP/1.0 200 OK
Date: Tue, 31 Dec 2013 08:28:44 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=38fb52774c793250:FF=0:TM=1388478524:LM=1388478524:S=8ETV1rNvlBXFrGms; expires=Thu, 31-De

How can I edit the code in order to return the html code on the page in addition to the HTTP Session info above?

Was it helpful?

Solution

The code uses a buffer of 256 bytes, which is often too small to store both the HTTP header and the actual HTML page. You should either user a larger buffer or read multiple times from the socket.

Edit

I think replacing the last few lines of the code (after the last write) by

do
{
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
        error("ERROR reading from socket");
    printf("%s",buffer);
} while (n > 0);

printf("\n");

will do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top