/* * This function report the error and * exits back to the shell: */ static void bail(const char *on_what) { if(errno != 0) { fputs(strerror(errno),stderr); fputs(": ",stderr); } fputs(on_what,stderr); fputc('\n',stderr); exit(1); }
int main(int argc,char **argv) { int z; char *srvr_addr = "127.0.0.1:9090"; struct sockaddr_in adr_srvr; /* AF_INET */ struct sockaddr_in adr_clnt; /* AF_INET */ int len_inet; /* length */ int s = -1; /* Socket */ int c = -1; /* Client Socket */ FILE *rx = NULL; /* Read Stream */ FILE *tx = NULL; /* Write Stream */ char buf[4096]; /* I/O buffer */ pid_t PID; /* Process ID */
/* * set signal hander for SIGCHLD: */ signal(SIGCHLD,sigchld_hander);
/* * User a server address from the command * line,otherwise default to 127.0.0.1: */ if(argc>=2) { srvr_addr = argv[1]; }
len_inet = sizeof adr_srvr; z = mkaddr(&adr_srvr,&len_inet, srvr_addr,"tcp");
if(z<0 || !adr_srvr.sin_port) { fprintf(stderr,"Invalid server " "address, or no port number " "was specified.\n"); exit(1); }
/* * Create a TCP/IP socket to use: */ s = socket(PF_INET,SOCK_STREAM,0); if(s==-1) bail("socket(2)");
/* * Bind the server address: */ z = bind(s,(struct sockaddr *)&adr_srvr,len_inet); if(z==-1) bail("bind(2)");
/* * make it a listening socket: */ z = listen(s,10); if(z==-1) bail("listen(2)");
/* * Start the server loop: */ for(;;) { /* * wait for a connect: */ len_inet = sizeof adr_clnt; c = accept(s,(struct sockaddr *)&adr_clnt,&len_inet); if(c==-1) bail("accept(2)");
/* * For a new server process * to service this client: */ if((PID = fork()) == -1) { /* Failed to fork:Give up */ close(c); continue; } else if(PID > 0) { /* parent process: */ close(c); continue; }
有时需要进行测试来查看一个指定的文件描述符是否在这个集合中(也就是查看相应的位是否置为1)。要测试是否设置了套接口c,我们可以编写下面的代码: int c; /* Client socket */ fd_set read_socks; /* Read set */ ... if ( FD_ISSET(c,&read_socks) ) { /* Socket c is in the set */ ... } else { /* Socket c is not in the set */ ... }
/* * This function rports the error and * exits back to the shell: */ static void bail (const char *on_what) { if(errno != 0) { fputs(strerror(errno),stderr); fputs(": ",stderr); } fputs(on_what,stderr); fputc('\n',stderr); exit(1); }
/* * Process client c: */ static int process_client(int c) { char buf[4096]; /* I/O Buffer */ FILE *rx = client[c].rx; FILE *tx = client[c].tx;
/* * Mail Program: */ int main(int argc,char **argv) { int z; char *srvr_addr = "127.0.0.1:9090"; struct sockaddr_in adr_srvr; /* AF_INET */ struct sockaddr_in adr_clnt; /* AF_INET */ int len_inet; /* length */ int s = -1; /* Socket */ int c = -1; /* Client Socket */ int n; /* return val from select */ int mx; /* max fd+1 */ fd_set rx_set; /* Read set */ fd_set wk_set; /* working set */ struct timeval tv; /* timeout value */
/* * Check if a connect has occured: */ if(FD_ISSET(s,&wk_set)) { /* * Wait for a connect: */ len_inet = sizeof adr_clnt; c = accept(s,(struct sockaddr *)&adr_clnt,&len_inet); if(c==-1) bail("accept(2)");
/* * See if we've exceeded server * capacity.if so,close the * socket and wait for the * next event: */ if(c>=MAX_CLIENTS) { close(c); continue; }