-
http connection lingers
Why does the http protocol linger at the end of the data transmission and how do you handle this.
I submit an HTTP GET request to a host, via sockets
I start a while loop using read() but at the end of the server's transmission read lingers, blocked I/O so to speak.
How should I go about handling this, right now I am using SIGALRM with a timer of 6 seconds RT but this is INCREDIBLY ineffective, how do normal browsers and other clients handle this anomaly?
Code:
while((n=read(connfd,buf,MAXLINE))>0)
{
printf("%d bytes read\n",n);
buf[n]=0; /* terminate string */
if (write(outfd,buf,n)<=0)
{
fprintf(stderr,"write() error\n");
return 0;
}
}
it doesn't get stuck in the while, it gets stuck in the read(). I would like to know how I should handle these situations, I am thinking of using non-blocking I/O but there are issues with that as well.
-
Send the "Connection: close" header to defeat Keep-Alive.
-
When keep-alive is in use, isn't it required that the Content-Length header be provided? Otherwise the client can't tell the difference between a stalled connection and the end of data.
So, the solution is to read exactly Content-Length bytes from the stream, then you know you are done and can close the socket.
-
yeah, the way to handle it is with the
Connection: close
in the GET Request
I just figured that out while playing with netcat.