How the server assigns port numbers to multiple clients

Source: Internet
Author: User
Tags htons

On a single computer, when using socket communication, the connection of different processes to differentiate network traffic relies on three parameters: protocol used for communication, address IP, port number.

For the server side, after bind, listen, and then accept to establish a new connection, accept returns a handle to the new connection, thus establishing a connection. So is the port number used by the newly established connection the same as the port number used by listen? I used to think that the server would randomly assign a new port number to use and later found it wrong.

Because 1, now using multi-channel IO multiplexing epoll, and so on, the server can be configured better to support hundreds of thousands of concurrent connections, the port number is 16-bit, up to 2^16-1, and with some commonly used port number can not be used, the available port number is not so much. 2. Most servers now use firewalls, and firewalls are only available for specific ports. If accept randomly assigns a port number, it will not pass through the firewall.

The TCP/IP protocol is an end-to-end protocol, it is only responsible for sending the data to the end, delivered to the upper level. Transport layer TCP, UDP plus the port number, the purpose is to distinguish between different applications. TCP also realizes the flow control, reliable transmission, and so on, and UDP just should not be the IP layer of data processing.

In the past, I know that an application can use only one port number, and if the handle returned by the accept or use the port number of listen, then how to implement the communication? If you establish multiple connections, how does the application receive information from which client?

It is now understood that the handle that is returned by the Accept connection consists of four parts: source IP, source port number, destination IP, destination port number. In one application, even if a connection is established with multiple clients, the application can differentiate between the destination IP and the destination port number when the data is received.

Verify with an echo server that both the client and the server are on the same machine:

The server listens on port 8000, and when the connection is not established, it can be seen in listening 8000


After establishing a connection through a client, you can see that a connection is established, the port number on the server side is 8000, or 8000 is monitored.


In connection with a client, you can see the establishment of two connections, the server side is 8000, listening or 8000.


Here is the server-side code:

#include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <errno.h> #include < Netinet/in.h>void str_echo (int sockfd) {int N;char buf[1024];again:while (n=read (sockfd,buf,1024)) >0) {Write ( Sockfd,buf,n);} if (n<0&&errno==eintr) goto again;else if (n<0) printf ("Str_error:read error");} int main (int argc, char **argv) {int listenfd, connfd;pid_t childid;socklen_t clilen;struct sockaddr_in cliaddr, servaddr; Listenfd=socket (af_inet, sock_stream, 0); Bzero (&servaddr,sizeof (SERVADDR)); servaddr.sin_family=af_inet; Servaddr.sin_addr.s_addr=htonl (inaddr_any);//host ipservaddr.sin_port=htons (8000);//portbind (LISTENFD, (struct sockaddr*) &servaddr,sizeof (struct sockaddr)) listen (listenfd,5);//up to five listeners for (;;) {clilen=sizeof (CLIADDR);//The entry is blocked on the Accept connfd=accept (LISTENFD, (struct sockaddr*) &cliaddr,&clilen); Childid=fork () ==0)//child{close (LISTENFD);//Turn off the Listener Str_echo (CONNFD) in the subprocess,//process The Listening connection exit (0);}} Close (LISTENFD);}

Client code:

 #include < stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h>void str_cli (FILE *FP , int sockfd) {char sendline[1024],recvline[1024];while (fgets (Sendline, 1024x768, FP)!=null) {//Send to Serverwrite (SOCKFD, Sendline,sizeof (Sendline));//Read Serverif (read (sockfd,recvline,1024) ==0) {printf ("Str_cli:server terminated Prematurely\n "); exit (0);} Fputs (recvline,stdout);p rintf ("%s", Recvline);}} int main (int argc, char **argv) {int sockfd;struct sockaddr_in servaddr;if (argc!=2) {printf ("usage:client IPaddress \ n"); Exit (0);} Sockfd=socket (af_inet, sock_stream, 0); Bzero (&servaddr,sizeof (SERVADDR)); servaddr.sin_family=af_inet; Servaddr.sin_port=htons (8000);//The input IP is stored in sin_addr Inet_pton (af_inet,argv[1],&servaddr.sin_addr);p rintf (" Server ip:%s\n ", argv[1]);//Establish connection connect (SOCKFD, (struct sockaddr*) &servaddr,sizeof (struct sockaddr)); STR_CLI ( STDIN,SOCKFD); exit (0);} 


How the server assigns port numbers to multiple clients

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.