#include<sys/time.h>#include<sys/types.h>#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<sys/socket.h>#include<netinet/in.h>#include<netdb.h>#definePORT8999intopen_socket_out(char*host,intport){inttype=SOCK_STREAM;structsockaddr_insock_out;intres;structhostent*hp;res=socket(PF_INET,type, 0);if(res== -1) {return-1; }hp=gethostbyname(host);if(!hp) {fprintf(stderr,"unknown host: %s\n",host);close(res);return-1; }memcpy(&sock_out.sin_addr,hp->h_addr,hp->h_length);sock_out.sin_port=htons(port);sock_out.sin_family=PF_INET;if(connect(res,(structsockaddr*)&sock_out,sizeof(sock_out))) {fprintf(stderr,"failed to connect to %s - %s\n",host,strerror(errno));close(res);return-1; }returnres;}staticintopen_socket_in(inttype,intport,structin_addr*address){structhostent*hp;structsockaddr_insock;charhost_name[1000];intres;intone=1;/* get my host name */if(gethostname(host_name,sizeof(host_name)) == -1) {fprintf(stderr,"gethostname failed\n");return-1; }/* get host info */if((hp=gethostbyname(host_name)) == 0) {fprintf(stderr,"gethostbyname: Unknown host %s\n",host_name);return-1; }memset((char*)&sock,0,sizeof(sock));memcpy((char*)&sock.sin_addr,(char*)hp->h_addr,hp->h_length);sock.sin_port=htons(port);sock.sin_family=hp->h_addrtype;if(address) {sock.sin_addr= *address; }else{sock.sin_addr.s_addr=INADDR_ANY; }res=socket(hp->h_addrtype,type, 0);if(res== -1) {fprintf(stderr,"socket failed\n");return-1; }setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char*)&one,sizeof(one));/* now we've got a socket - we need to bind it */if(bind(res, (structsockaddr* ) &sock,sizeof(sock)) == -1) {fprintf(stderr,"bind failed on port %d\n",port);close(res);return-1; }returnres;}voidset_nonblocking(intfd){unsignedv=fcntl(fd,F_GETFL, 0);fcntl(fd,F_SETFL,v|O_NONBLOCK);}staticvoidchild(intfd_in){intfd;structsockaddraddr;intin_addrlen=sizeof(addr);if(listen(fd_in, 5) == -1) {close(fd_in);exit(1); }fd=accept(fd_in,&addr,&in_addrlen);sleep(20);exit(0);}staticvoidparent(void){intfd=open_socket_out("localhost",PORT);fd_setfset;charc=0;intcount=0;set_nonblocking(fd);while(1) {FD_ZERO(&fset);FD_SET(fd, &fset);if(select(8,NULL, &fset,NULL,NULL) == 1) {if(write(fd, &c, 1) == -1 && (errno==EAGAIN||errno==EWOULDBLOCK)) {printf("select returns when write would block\n");exit(1); }count++;if(count% 1024 == 0) {printf("%d\n",count); } } }}intmain(void){intfd_in;fd_in=open_socket_in(SOCK_STREAM,PORT,NULL);fork() ?parent() :child(fd_in);return0;}