2

Take for example, a simple web server in C's loop:

while(1)
{
    /*accept connection that comes into server_socket and assign
      its handle to client_socket*/
    client_socket = accept(server_socket, NULL, NULL);

    /*send data to the client thru the client_socket*/
    send(client_socket, http_header, sizeof(http_header), 0);
    close(client_socket);
}     

On a deeper level, what stops, say, an incoming connection from hitting the port at the instant that this loop is now past the client_socket = accept() portion but before the close() portion, and the connection doesn't get seen/accepted?

Is the underlying client connection socket code generally firing a bunch of requests at the server until one is "caught" and then the connection has been made?

A similar analogy to what I'm asking is say I was running around a 400m track over and over, making a lap every 2 minutes. While I'm on the opposite side of the track, my friend comes by to try and give me a high five, but I am way on the other side. 10 seconds later, he leaves, so thus I've "missed" him. So what stops this type of thing from happening in the context of a while loop/socket connection like this, especially where there may be more code than this simple program?

4

There is a thing called "backlog" which is a queue in which incoming connections temporarily stay until you have had a chance to accept them. It exists precisely so as to allow you to spend some time doing stuff between successive calls to accept.

The length of the backlog is a parameter to the listen() method, which is where you got your server_socket from. If the value that you specify is too small, and a large number of simultaneous requests to connect are received, you risk losing some connections due to appearing busy.

Once you know how it is called, you can look it up and find very interesting articles explaining precisely how it works, like this one:

http://veithen.github.io/2014/01/01/how-tcp-backlog-works-in-linux.html

Also, in case you are wondering what value to specify for backlog, there is this related stackoverflow question: https://stackoverflow.com/questions/10002868/what-value-of-backlog-should-i-use

3

Listening on a port isn't like running laps around a track and trying to high-five your friend. It's more like running laps around your apartment complex and checking the mailbox every time around. Occasionally when you come around, someone has dropped something in your mailbox, so you pick it up and do something with it.

In the case of ports / sockets, the hardware gets a message on a certain port (in the case of web servers, typically port 80 or 443). Then it says, "ok, who is interested in this?" and then sends a hardware interrupt to your program and drops the "message" in your "mailbox". Then your code picks it up the next time around the loop and does it's thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.