Understanding TCP listening socket

99 Views Asked by At

I've recently started studying socket implementation in C and I'm confused on how a TCP server is implemented. From what I've understood the server has a listening socket where it accepts connection requests from the clients and then creates a new socket for each client to send and receive data. If that's the case when and where does the TCP handshake happen to start a connection (between the client and the listening socket to create the new one ? ) Could somebody clarify this concept for me ?

1

There are 1 best solutions below

0
Ágatha On

The "magic" of TCP handshake here happens in the operating system's kernel.

The POSIX API has socket functions to abstract all that low-level part off your code, so you don't have to worry about TCP handshake, packet manipulation, etc.

From the user-space, all you need to know is that you have a file descriptor to read from and/or write to, the user-space application is not concerned with the handshake or if there's any, unless you're working with kernel-bypass - but in that case you won't be using the POSIX socket API.

Of course, if you want to know, you'll need to take a look at the kernel code that provides those functions, since it's implementation specific (although these implementations won't be much different from each other).