wpark
wpark

Reputation: 105

How can I detect if http request come from same client?

I am making http/1.1 server for understanding better how webserver works.

I saw that http/1.1 version support "keep-alive" as default for network improvement, so I am trying to figure out how can I implement it...

For exemple, if client access one page, and send these two request (one for index.html for the first time, and other for favicon), my server make two socket per request regardless from which client.

select(...);
FD_ISSET(fd, &read_set);
if (fd == server_socket)
{
    // here, i want to add code
    // if request come from same client and socket is still alive, 
    // I want to reuse it without creating new socket again.
    ...

    // else if request is another new client, then make connection 
    new_socket = accept(fd, (sockaddr *)&new_address, &new_len);
}

How can I detect if the client is same client or not ? Thanks for reading.

Upvotes: 0

Views: 447

Answers (1)

user2146414
user2146414

Reputation: 1038

I think you mix two things: server socket is used to accept incoming connections. new_socket is used to read data from. If you want to handle keep-alive then you should not close the new_socket but wait for data on that socket using select

Upvotes: 2

Related Questions