From: Lyle Wincentsen [mailto:lyle.wincentsen@xxxxxxxxx] Sent: Wednesday, June 03, 2009 10:53 PM To: users@xxxxxxxxxxxxxxxx Subject: Re: http/1.1 persistent connection issue. On Wed, Jun 3, 2009 at 2:25 AM, <vincent.blondel@xxxxxx wrote: Hello all, I am trying to develop a little program that should be able to send http/1.1 requests. This is all running fine but I get some problems with the persistent connection. I mean I can send one request in my http connection but the second one does not seem to work properly. Maybe I made an error in my code, maybe I do not understand something in http/1.1 ? Can somebody help me please ? ... many thks. ----- source code ------- #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <strings.h> #include <stdio.h> #include <stdlib.h> const static char GET [] = { "GET / HTTP/1.1\r\n" "Host: 127.0.0.1\r\n" "User-Agent: UserAgent\r\n" "Connection: keep-alive\r\n" "\r\n" }; int main(int argc, char **argv) { int bufsize = 0; char buffer[1024] = "\n"; int32_t i32SocketFD = socket(PF_INET, SOCK_STREAM, 0); if (-1 == i32SocketFD) { fprintf(stderr, "Error i32SocketFD"); exit(-1); } struct sockaddr_in stSockAddr; bzero(&stSockAddr, sizeof(stSockAddr)); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(80); int32_t i32Res = inet_pton(AF_INET, "127.0.0.1", (void *) &stSockAddr.sin_addr); if (0 i32Res){ fprintf(stderr, "i32Res"); exit(-1); } else if (0 == i32Res){ fprintf(stderr, "Error i32Res 2"); exit(-1); } if (-1 == connect(i32SocketFD, (struct sockaddr *) &stSockAddr, sizeof(stSockAddr))) { fprintf(stderr, "Error connect"); exit(-1); } if (send(i32SocketFD, GET, strlen(GET), 0) == -1) { fprintf(stderr, "Error sending data."); } while((bufsize = read(i32SocketFD, buffer, sizeof(buffer) - 1))){ printf("%s",buffer); } if (send(i32SocketFD, GET, strlen(GET), 0) == -1) { fprintf(stderr, "Error sending data."); } while((bufsize = read(i32SocketFD, buffer, sizeof(buffer) - 1))){ printf("%s",buffer); } shutdown(i32SocketFD, 2); close(i32SocketFD); } It might be helpful if you included the stderr output, or whatever indications you are getting that it "does not seem to work properly". -- Lyle, First of all, many thks to help me. Now what and how do I test my little code. Simply I get an apache running at localhost on my debian machine with a default apache website. Concerning the code above, you see I send two requests in my socket so I expect to receive the html code two times on my screen and this is not. I simply see the code ONLY one time. If I remove "while .. bufsize .. read .. printf" block code I see two requests coming in my apache logs but the second request return 0 bytes .. This is why I look on the net for finding a very little http/1.1 example source code sending multiple requests in the same persistent socket connection but nothing found till now. I found http/1.0, http/1.1 sending just one request. The other ones ( e.g firefox, ... ) are really too long and complex for my knowlegde for getting an idea how to proceed to send multiple requests in the same socket connection. Below the result when sending one request in my socket connection but how can I do to get all objects of one page in the same socket .. let we take the example of one page containing the html, the css and the javascript pages ? ----- HTTP/1.1 302 Found Date: Thu, 04 Jun 2009 05:45:26 GMT Server: Apache/2.2.3 (Debian) mod_auth_kerb/5.3 mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch15 mod_perl/2.0.2 Perl/v5.8.8 Location: http://127.0.0.1/apache2-default/ Content-Length: 392 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://127.0.0.1/apache2-default/">here</a>.</p> <hr> <address>Apache/2.2.3 (Debian) mod_auth_kerb/5.3 mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch15 mod_perl/2.0.2 Perl/v5.8.8 Server at 127.0.0.1 /address> </body></html> ----------------------------------------------------------------- ATTENTION: The information in this electronic mail message is private and confidential, and only intended for the addressee. Should you receive this message by mistake, you are hereby notified that any disclosure, reproduction, distribution or use of this message is strictly prohibited. Please inform the sender by reply transmission and delete the message without copying or opening it. Messages and attachments are scanned for all viruses known. If this message contains password-protected attachments, the files have NOT been scanned for viruses by the ING mail domain. Always scan attachments before opening them. ----------------------------------------------------------------- --------------------------------------------------------------------- The official User-To-User support forum of the Apache HTTP Server Project. See <URL:http://httpd.apache.org/userslist.html> for more info. To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx " from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx