address multiplexing in the Socket API
The server side uses so_reuseaddr whenever possible, calling setsockopt to set SO_REUSEADDR socket options before binding. This option allows you to restart the server without having to wait for the time_wait state to disappear.
Synopsis #include <sys/types.h> /* See NOTES * /#include <sys/socket.h> int getsockopt ( int SOCKFD, int level, int optname, void *optval, socklen_t *optlen); int setsockopt (int sockfd, int level, int optname, const void *optval, socklen_t optlen);
You can add code before bind (complete code, please refer to the end of the blog post):
int optval = 1; if (setsockopt (sockfd,sol_socket,so_reuseaddr,&optval,sizeof (optval)) = =-1) { err_exit ("setsockopt so _REUSEADDR error "); }
Used to support address reuse.
Socket Service Multiple concurrency-multi-Client connection
As can be seen, the biggest disadvantage of our server is unable to support multi-client connection, even if the client can connect to the server, the server does not do services for the customer (direct no response), although the link is there (that is, the client is already connected to the server, but the server is not responding to you ....)
From the server below this code we can see the clue ....
struct sockaddr_in peeraddr; Socklen_t Peerlen; Note: Only one connection can be processed at a time int peersockfd = Accept (SOCKFD, (struct sockaddr *) &peeraddr,&peerlen); if (PEERSOCKFD = =-1) { err_exit ("Accept Error"); } Print customer information cout << "client:" << Endl; cout << "\tsin_port:" << ntohs (peeraddr.sin_port) << Endl; cout << "\tsin_addr:" << inet_ntoa (peeraddr.sin_addr) << Endl; cout << "\tsocket:" << peersockfd << Endl; //.....
That is, the server runs once and can only serve one client at a time!!!!
Concurrent Server implementations
1. The client sends a request to the server
2. Typical (multi-process) Concurrent Server program framework
Complete server-side code # include "Commen.h" int main () {int SOCKFD = socket (af_inet,sock_stream,0); if (SOCKFD = =-1) {Err_exit ("socket error"); } int optval = 1; if (setsockopt (sockfd,sol_socket,so_reuseaddr,&optval,sizeof (optval)) = =-1) {err_exit ("setsockopt so_reuse ADDR error "); } struct sockaddr_in serveraddr; serveraddr.sin_family = af_inet; Serveraddr.sin_port = htons (8002); SERVERADDR.SIN_ADDR.S_ADDR = Inaddr_any; Bind any IP address of this machine//SERVERADDR.SIN_ADDR.S_ADDR = inet_addr ("10.255.218.20"); if (bind (SOCKFD, struct sockaddr *) &serveraddr,sizeof (serveraddr)) = =-1) {Err_exit ("bind error"); }//Once the listen is called, the SOCKFD programming passive socket: Waiting for the client to connect (only accept the connection, cannot send the connection) if (listen (sockfd,somaxconn) = = 1) {err_exit ("lis Ten error "); } struct sockaddr_in peeraddr; Socklen_t Peerlen; while (true) {int peersockfd = accept (SOCKFD, (struct sockaddr *) &peeraddr,&peerlen); if (PEERSOCKFD = =-1) {Err_exit ("accept error"); }//Print customer information cout << "client:" << Endl; cout << "\tsin_port:" << ntohs (peeraddr.sin_port) << Endl; cout << "\tsin_addr:" << inet_ntoa (peeraddr.sin_addr) << Endl; cout << "\tsocket:" << peersockfd << Endl; Each client connection comes in, a sub-process is fork,//The corresponding business process is completed by the child process, and the parent process continues to listen pid_t PID = fork (); if (PID = =-1) {close (SOCKFD); Close (PEERSOCKFD); Err_exit ("fork Error"); } else if (PID = = 0)//Sub-process, processing business {close (SOCKFD); The child process closes the listener socket because the child process is not responsible for the listener task Char Buf[bufsiz]; memset (buf,0,sizeof (BUF)); ssize_t readcount = 0; while ((Readcount = Read (peersockfd,buf,sizeof (BUF))) >= 0) {//If read returns 0 during data reading, the other side is closed Connect if (Readcount = = 0) {Err_exiT ("read PEERSOCKFD error"); } if (write (peersockfd,buf,readcount) = =-1) {Err_exit ("Write PEERSOCKFD Error "); } Buf[readcount] = ' + '; Fputs (buf,stdout); memset (buf,0,sizeof (BUF)); }} else if (PID > 0)//parent process {Close (PEERSOCKFD); The parent process closes the connection socket because the parent process is not responsible for the child Process Service}} close (SOCKFD); return 0;}
Socket Programming Practice (4)