#include #include #include #include #include #include #include #define PROTOPORT 5050 /* default protocol port number */ #define QLEN 6 /* size of request queue */ main(argc, argv) int argc; char *argv[]; { struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad; /* structure to hold server's address */ struct sockaddr_in cad; /* structure to hold client's address */ int sd, sd2; /* socket descriptors */ int port; /* protocol port number */ int alen; /* length of address */ memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)PROTOPORT); if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { fprintf(stderr, "cannot map \"tcp\" to protocol number"); exit(1); } /* Create a socket */ sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { fprintf(stderr, "socket creation failed\n"); exit(1); } /* Bind a local address to the socket */ if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { fprintf(stderr,"bind failed\n"); exit(1); } /* Specify size of request queue */ if (listen(sd, QLEN) < 0) { fprintf(stderr,"listen failed\n"); exit(1); } signal(SIGCHLD, SIG_IGN) ; /* Main server loop - accept and handle requests */ while (1) { alen = sizeof(cad); if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) { fprintf(stderr, "accept failed\n"); exit(1); } if (fork()) { /* I am the parent! */ close(sd2) ; } else { /* I am the child! */ close(sd) ; dup2(sd2, 0) ; dup2(sd2, 1) ; close(sd2) ; execl("/usr/users/brock/classes/363/f99/examples/echoline", "echoline", NULL) ; printf(stderr, "Should never get here\n") ; } } }