Reusing Client TCP Connection for HTTP
Why can't I reuse a http client socket in a C web client, since I don't call close(http_socket_fd)
? The first write/read to the socket file descriptor works perfectly well. Any/all successive reads return zero (hardly any error).
Basically, I don't want to keep recreating new client connection sockets to the same host for successive requests. Is it not possible in C to reuse an open client socket (which already has HTTP keep-alive enabled) for successive read/writes? It seems possible with Java http://www.mail-archive.com/httpclient-dev@jakarta.apache.org/msg04687.html
Example: (PSEUDO_CODE)
MANY_DOMAINS=30,000;
//initial connection
do{
http_socket_to_domain_x=open(NEW_TCP_SOCKET_PER_DOMAIN);
get(initial_url_path);
read(http_socket_to_domain_x,initial_http_response);
} while(EACH_DOMAIN)开发者_如何转开发
for(LIST_OF_URLS FROM EACH_DOMAIN);
//successive connections - NO RECREATING TCP SOCKET!
do{
get(another_url_path);
read(http_socket_to_domain_x,another_http_response);
} while(EACH_URL_PER_DOMAIN)
//finally
close(http_socket_to_domain_x);
You want to read up on persistent HTTP/1.1 connections.
The server may be closing the connection. Are you sending HTTP/1.1 headers? See http://www.io.com/~maus/HttpKeepAlive.html for a bit more.
精彩评论