简体   繁体   中英

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);

您想阅读持久性HTTP / 1.1连接

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM