http connection lingers

This is a discussion on http connection lingers within the Networking/Device Communication forums, part of the General Programming Boards category; Why does the http protocol linger at the end of the data transmission and how do you handle this. I ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    9

    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.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,656
    Send the "Connection: close" header to defeat Keep-Alive.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    9
    yeah, the way to handle it is with the
    Connection: close
    in the GET Request

    I just figured that out while playing with netcat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 12:45 PM
  2. C# HTTP request/response
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-02-2008, 07:00 AM
  3. programming with HTTP connection through GPRS
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 12-09-2006, 06:32 AM
  4. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 10:18 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21