Ref: https://www.cs.dartmouth.edu/~campbell/cs50/socketprogramming.html
Data flow between wire and app
Established Socket
A socket is created by an application running in a host. The application assigns a transport protocol (TCP or UDP) and source and destination addresses to the socket. It identifies sockets by assigning numbers to them.
Note the web server has two sockets opened: one for each web page it is serving. These sockets are differentiated by the destination port numbers.
[!NOTE]
One host does not assign the socket number on both sides of the communication channel. The socket numbers assigned to each socket are only used by the host that assigned them. In other words, socket number 1 created on one host may be connected to socket number 5 on another host.
[!NOTE]
Based on the Well-Known source port numbers assigned to each socket, we can determine sockets 1 and 2 were created by an HTTP server application and socket 3 was created by an SMTP or email server application.
Sockets numbers may not be same on both sides
This graphic shows a virtual TCP connection between a client and server. Note the socket numbers are not the same on both sides of the channel. Hosts create, close and number their own sockets.
Sockets
TCP
The accept()
function creates a new socket from the first connection request for the specified socket_descriptor
and returns the file descriptor of the new socket. The file descriptor of this new socket is used in the read()
and write()
functions to send and receive data to and from the client node.
Ref: https://people.cs.rutgers.edu/~pxk/417/notes/index.html