CodePudding user response:
#include#include
#include
#include
#include
#include
#include
#include
# define MAXLINE 50
Typedef struct sockaddr SA;
Int main (int arg c, char * * argv)
{
Int listenfd, connfd, maxfd, I, nbyte;
Struct sockaddr_in myaddr;
Char buf [MAXLINE];
Fd_set global_rdfs current_rdfs;
If ((listenfd=socket (AF_INET SOCK_STREAM, 0)) & lt; 0)
{
Perror (" fail to socket ");
The exit (1);
}
Bzero (& amp; Myaddr, sizeof (myaddr));
Myaddr. Sin_family=AF_INET;
Myaddr. Sin_port=htons (8888);
//myaddr. Sin_addr. S_addr=inet_addr (" 127.0.0.1 ");
Myaddr. Sin_addr. S_addr=htonl (INADDR_ANY);
If (bind (listenfd, (SA *) & amp; Myaddr, sizeof (myaddr)) & lt; 0)
{
Perror (" bind fail ");
Close (listenfd);
The exit (1);
}
Listen (listenfd, 5);
FD_ZERO (& amp; Global_rdfs);
FD_SET (listenfd, & amp; Global_rdfs);
Maxfd=listenfd;
While (1)
{
Current_rdfs=global_rdfs;
Printf (" before the select \ n ");
If (select (maxfd + 1, & amp; Current_rdfs, NULL, NULL, 0) & lt; 0)
{
Perror (" select fail ");
Close (listenfd);
The exit (1);
}
The else
{
Printf (" after the select \ n ");
For (I=0; i <=maxfd; I++)
{
If (FD_ISSET (I, & amp; Current_rdfs))
{
If (I==listenfd)
{
Connfd=accept (listenfd, NULL, NULL);
FD_SET (connfd, & amp; Global_rdfs);
Maxfd=maxfd & gt; Connfd? Maxfd: connfd;
}
The else
{
Nbyte=recv (I, buf, sizeof (buf), 0).
Buf [nbyte]=0 x00;
If (nbyte & lt;=0)
{
Close (I);
FD_CLR (I, & amp; Global_rdfs);
}
The else
{
//buf [nbyte]=0 x00;
Send (I, buf, nbyte, 0).
}
}
}
}
}
}
return 0;
}
CodePudding user response:
Can implement a server at the same time more than one customer to accept the request, specific response can be modified, here with the function of echo, want to consider a variety of concurrent server, a careful analysisCodePudding user response:
Establish communication between two processes,A process binding port, and provide a callback interface,
Another process registered interface.