41 serverAddr=(
struct sockaddr_in *)malloc(
sizeof(
struct sockaddr_in));
56 serverSock = socket (AF_INET, SOCK_STREAM, 0);
58 fprintf(stderr,
"can't create socket\n"); exit (1);}
59 stat=bind (serverSock,
serverAddr,
sizeof(
struct sockaddr_in));
61 printf (
"bind failed, errno = %d\n",
errno);
63 if (listen (serverSock, 2) <0 ) {
64 printf (
"listen failed\n");
67 printf(
"listening on port= %d sock=%d\n", port, serverSock);
74 {
struct sigaction sigabort;
75 struct sockaddr_in *server_addr, client_addr;
78 long ports, exceptions, sockbits;
79 struct timeval time_out;
86 if (
argc>1) sscanf(argv[1],
"%d",&port_no);
88 if (
argc>2) sscanf(argv[2],
"%d",&debug);
91 signal(SIGPIPE, SIG_IGN);
97 sockbits=bitset(1, server_port);
103 ports=sockbits; exceptions=sockbits;
104 stat=select(32, &ports,0, &exceptions, &time_out);
108 fprintf(stderr,
"select: %d count=%d ready=%x excpt=%x \n",
109 transaction_count, stat, ports, exceptions);
114 if (bittest(exceptions,i)) {
115 fprintf(stderr,
"exception from %d\n", i);
116 sockbits=bitclr(sockbits,i);}
119 if (bittest(ports,server_port)) {
120 client_len =
sizeof (
struct sockaddr_in);
121 snew = accept (server_port, &client_addr, &client_len);
122 if (snew>0) sockbits=bitset(sockbits, snew);
123 ports=bitclr(ports,server_port);
124 fprintf(stderr,
"new connection %d is accepted \n", snew);
127 if (bittest(ports, 0)) {
129 size=read(0,
buf,65536);
130 if (snew) write(snew,
buf,size);}
131 if (bittest(ports, snew)) {
133 size=read(snew,
buf,65536);
134 if (snew) write(1,
buf,size);}