On Wed, Jun 3, 2009 at 2:25 AM, <vincent.blon...@ing.be> 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".

Reply via email to