3

Let's say we have a server that can accept multiple clients. First, it has to create a socket, then bind it with a port and an IP and finally listen to requests for connection from clients. After accept()ing a connection with a client, the server creates a new socket to communicate with the specific client. My question is whether or not the client is going to send its data to the same port it sent its initial request to, and if not how does it know where to send it?

BillTheKid
  • 377
  • 1
  • 13
  • Probably to the same port it sent the connection request. – kiner_shah Dec 18 '21 at 11:02
  • You can use e.g. `getsockname` to get the local details of a socket, like its port number. – Some programmer dude Dec 18 '21 at 11:02
  • Also remember that an endpoint is defined by three things: Address, protocol and port-number. And a connection is defined by two endpoints. This means that one endpoint can be "shared" between different connections, since the other end of the connection then uniquely defines the connection itself. – Some programmer dude Dec 18 '21 at 11:05
  • @Someprogrammerdude If I am getting this right, you are saying that there maybe be multiple connections associated with a port, and each server client can identify the data that are meant for it by making sure that the pair (client and itself) contains a its own client. But since there is a listening socket at that port, why doesn't it try to accept data from clients that have already been connected to the server? How does it know that their data are meant for another server process? – BillTheKid Dec 18 '21 at 11:16
  • 1
    Each packet sent over a connection have both its source and destination triple in it (address, protocol, port). The system can then use the source and destination triple as a unique connection. It then uses the destination triple to know which "program" on the local system to send the packet to, and uses the source triple to put the packet in the right queue. – Some programmer dude Dec 18 '21 at 11:22
  • Does this answer your question? [How does the socket API accept() function work?](https://stackoverflow.com/questions/489036/how-does-the-socket-api-accept-function-work) – stark Dec 18 '21 at 13:42
  • I've actually seen this post, but no, unfortunately it did not fully answered my question. Thanks for the recommendation! – BillTheKid Dec 18 '21 at 13:57

3 Answers3

4

A socket connection is uniquely identified by a tuple of [Protocol, Local IP, Local Port, Peer IP, Peer Port].

A TCP server creates a listening socket with a tuple of [TCP, Listen IP, Listen Port, 0, 0]. When a client requests to connect to a server, the network routes the request to the specified IP/Port. The receiving device then routes the request to a matching listening socket, performs a 3way handshake with the client, and puts it into a queue. Later, when accept() is called, it extracts the next pending client from the queue and returns a new socket identified with a tuple of [TCP, Listen IP, Listen Port, Client IP, Client Port]. Because of this, a single listening socket can accept multiple Clients from different Client IP/Port combinations.

A TCP client creates a connecting socket with a tuple of [TCP, Local IP, Local Port, 0, 0]. When the 3way handshake is complete, the socket's tuple is updated to [TCP, Local IP, Local Port, Server IP, Server Port]. Because of this, a Client can connect separate sockets to different Servers at differing Server IP/Port combinations.

All subsequent data exchanges use these tuples.

Data sent out from a Client's connecting socket will be sent to the associated Server IP/Port and stored in the buffer of the accepted Server socket whose tuple matches both the Client and Server.

Data sent out from a Server's listening socket will be ignored, since there is no associated Client.

Data sent out from an accepted Server socket will be sent to the associated Client IP/Port and stored in the buffer of the connected Client socket whose tuple matches the Client and Server.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

Generally There is always a default port allotted for each kind of communication.Operating System may kept it open or close ,it can be checked .

Let's say for FTP connection, There is a separate port allotted for handshake,It don't matter how many new FTP connection are being requested, all new connection will go to that same port , Once handshake is completed data exchange is done via another port, Even if we don't specify port. If Network manager has Port List entries earlier it will request to the same port.

Example for SSH if you request for

ssh -X <IP> 

Even if you don't mention port , Your system know which port to request for and at server side there is always some port open who will be listening to your request and based on data you send while handshake it will continue listening or block you.

Bonus is you can open your custom port at server side who will be listening to your request. TCP implementation by default declare which port will be used for what kind of communication.

Adiitrack
  • 11
  • 3
  • The OP is asking what happens after accept. Mentioning that FTP data connections use a different port here is confusing since this is no longer the original accept, but data connections are handled by a different listener + accept. And for this one again port stay the same. – Steffen Ullrich Dec 18 '21 at 12:41
  • when server accept request,it initiate data transmission via other port to client's port. Since server already know client IP as well as port by SYN packet (the start of a TCP connection) [Can be seen vis wireshark application ]. every TCP packet includes [source ip, destination ip, source and destination port and protocol , ....) One server confirm first packet communication, Both side know each other now Unless there is a change in any information[source IP, Destination IP , Source port, Destination Port ] . https://www.techtarget.com/searchnetworking/definition/port-number – Adiitrack Dec 18 '21 at 13:20
0

The client connects with a source IP and port to a server with a destination IP and port. After accept exactly the same IP and port on both sides are continued to be used for data exchange as for the establishment of the connection.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • How does the listening socket know not to try to accept again clients that have already been accepted? Is the message of the packet different? Is there some type of history/cache from the server side? – BillTheKid Dec 18 '21 at 11:27
  • 1
    @BillTheKid Perhaps you might need to spend a little more time reading about TCP and how it handles connections. Like for example learning about what a *three-way handshake* is. – Some programmer dude Dec 18 '21 at 11:28
  • @Someprogrammerdude Oh alright then! our conversation has been really useful! thank you for you time! – BillTheKid Dec 18 '21 at 11:31