简体   繁体   中英

question related to sockets in network programming with C

There is a system call known as socket(); it creates a socket on the listening server. What I want to understand is a server creates an IP+port combination. Let us say telnet uses port 23. Now when the client machines do connections then the port on which the server is listening then the connection there is not on port 23 in fact it is on a different port.My confusion is on the server side also does the same thing happen. For example I write a server to listen to port 23 then the connections which will be done on server side with different clients how are they differentiated because all of them will be on same port.So how can you make so many connections on the same server port.If some one uses telnet (23) or ftp (21) or ssh (22) then many people can still login to the same service port on the server ie more than one connection for ssh by different users where as ssh is listening only at port 22.So what exactly does the socket do or how is a socket created?

UPDATE

I got what is explained.Depending upon the IP+port combination from the client machine where the connection originated rest of the things by the server side can be handled and I think this information probably is used by socket file descriptors. What I see is in connect() system call which we use as follows

connect(sockfd,(struct sockaddr *)&client_address,size_t);

we do pass on struct sockaddr * at the client end which has unique IP+port combination I think when server gets this in accept then things proceed. What I want to know further is the argument at server side

accept(server_sockfd,(struct sockaddr *)&client_address,(size_t *)sizeof (struct sockaddr ));

Does it get that same client_address which from client side was passed on using connect() system call? If yes then the socket_descriptors for the same server listening to many clients are different.What I want to know is how does the data structure at the server side is maintained when it accepts a request from the client.

TCP connections are identified by 4 parameters:

  • local IP
  • local port
  • remote IP
  • remote port

So, even if two connections share the same local IP and port, the OS can differentiate between then because the remote IP and/or port are going to be different.

The unique combination that identifies the connection is:

  • Source address and port
  • Destination address and port

In your example the destination address and port are the same for many connections, but each comes from a unique combination of source address and port.

Here is a brief tcpdump session of me connecting from my desktop to my server via FTP (port 21):

22:55:50.160704 IP 172.17.42.19.64619 > 172.17.42.1.21: S 2284409007:2284409007(0) win 8192 <mss 1460,nop,nop,sackOK>
22:55:50.160735 IP 172.17.42.1.21 > 172.17.42.19.64619: S 1222495721:1222495721(0) ack 2284409008 win 65535 <mss 1460,sackOK,eol>
22:55:50.160827 IP 172.17.42.19.64619 > 172.17.42.1.21: . ack 1 win 8192
22:55:50.162991 IP 172.17.42.1.21 > 172.17.42.19.64619: P 1:61(60) ack 1 win 65535
22:55:50.369860 IP 172.17.42.19.64619 > 172.17.42.1.21: . ack 61 win 8132
22:55:56.288779 IP 172.17.42.19.64620 > 172.17.42.1.21: S 3841819536:3841819536(0) win 8192 <mss 1460,nop,nop,sackOK>
22:55:56.288811 IP 172.17.42.1.21 > 172.17.42.19.64620: S 454286057:454286057(0) ack 3841819537 win 65535 <mss 1460,sackOK,eol>
22:55:56.288923 IP 172.17.42.19.64620 > 172.17.42.1.21: . ack 1 win 8192
22:55:56.290224 IP 172.17.42.1.21 > 172.17.42.19.64620: P 1:61(60) ack 1 win 65535
22:55:56.488239 IP 172.17.42.19.64620 > 172.17.42.1.21: . ack 61 win 8132
22:56:03.301421 IP 172.17.42.19.64619 > 172.17.42.1.21: P 1:12(11) ack 61 win 8132
22:56:03.306994 IP 172.17.42.1.21 > 172.17.42.19.64619: P 61:94(33) ack 12 win 65535
22:56:03.510663 IP 172.17.42.19.64619 > 172.17.42.1.21: . ack 94 win 8099
22:56:06.525348 IP 172.17.42.19.64620 > 172.17.42.1.21: P 1:12(11) ack 61 win 8132
22:56:06.526332 IP 172.17.42.1.21 > 172.17.42.19.64620: P 61:94(33) ack 12 win 65535
22:56:06.726857 IP 172.17.42.19.64620 > 172.17.42.1.21: . ack 94 win 8099

You can see the initial connection is 172.17.42.19.64619 <-> 172.17.42.1.21 . Port 64619 is what the Windows 7 box happened to select as the source port when it made the outgoing connection. The two lines with S are the SYN packets going back and forth to establish the connection. Then I start the next connection and Windows just uses the next available port, 64620. The connection 172.17.42.19.64620 <-> 172.17.42.1.21 forms a new unique tuple of the items I listed at the top. Only the client's port is different, but that's enough. Each packet that arrives at the server to port 21 can be distinguished by the source port. Each packet from port 21 on the server arriving at the client can be distinguished by the destination port.

I will give you pseudo code from Qt to explain the matter. All TCP servers work in same way.

You first create a listening TCP server socket. When an incoming connection request arrives the operating system creates a new socket (your OS uses a different port than listening socket) and associates this new socket with the remote client for you. The TCP server socket continues to accept new connections and you resume your communication with the remote peer via the newly created socket.

tcpServer.listen(LocalHost, PORT);

connect(&tcpServer, SIGNAL(newConnection()),this, SLOT(NewConnection()));

//Callback that is called when server accepts a new connection
void NewConnection()
{

   //Here you will have a new socket for communication with the client
   //The tcpServer will continue listening to given port 

   QTcpSocket* connection = tcpServer.NextPendingConnection();

   //Use your new connection to communicate with the remote client...

}

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM