00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 serverAddr=(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
00042
00043 memset(serverAddr, 0, sizeof (*serverAddr));
00044 serverAddr->sin_family = AF_INET;
00045 serverAddr->sin_port = htons(port_no);
00046 serverAddr->sin_addr.s_addr = htonl(INADDR_ANY);
00047 return(serverAddr);
00048 }
00049
00050 int make_socket_port(port)
00051 int port;
00052 { struct sockaddr_in *serverAddr;
00053 int serverSock, stat;
00054
00055 serverAddr= make_socket_address(port);
00056 serverSock = socket (AF_INET, SOCK_STREAM, 0);
00057 if (serverSock < 0) {
00058 fprintf(stderr, "can't create socket\n"); exit (1);}
00059 stat=bind (serverSock, serverAddr, sizeof(struct sockaddr_in));
00060 if (stat<0){
00061 printf ("bind failed, errno = %d\n", errno);
00062 exit(1);}
00063 if (listen (serverSock, 2) <0 ) {
00064 printf ("listen failed\n");
00065 close (serverSock);
00066 exit (1);}
00067 printf("listening on port= %d sock=%d\n", port, serverSock);
00068 return(serverSock);}
00069
00070
00071 main (argc,argv)
00072 int argc;
00073 char *argv[];
00074 { struct sigaction sigabort;
00075 struct sockaddr_in *server_addr, client_addr;
00076 int server_port;
00077 int port_no;
00078 long ports, exceptions, sockbits;
00079 struct timeval time_out;
00080 char buf[65536];
00081 int snew=0;
00082 int i;
00083 int stat;
00084 long client_len;
00085
00086 if (argc>1) sscanf(argv[1],"%d",&port_no);
00087 else port_no=5000;
00088 if (argc>2) sscanf(argv[2],"%d",&debug);
00089
00090 signal(SIGINT, exit);
00091 signal(SIGPIPE, SIG_IGN);
00092
00093
00094 server_port=make_socket_port(port_no);
00095
00096
00097 sockbits=bitset(1, server_port);
00098 time_out.tv_sec=10;
00099 time_out.tv_usec=0;
00100
00101 while (1) {
00102 select_again:
00103 ports=sockbits; exceptions=sockbits;
00104 stat=select(32, &ports,0, &exceptions, &time_out);
00105 if (stat>0) {
00106 transaction_count++;
00107 if (debug>2)
00108 fprintf(stderr, "select: %d count=%d ready=%x excpt=%x \n",
00109 transaction_count, stat, ports, exceptions);
00110 if (exceptions) {
00111
00112 i=0;
00113 while (i<32) {
00114 if (bittest(exceptions,i)) {
00115 fprintf(stderr, "exception from %d\n", i);
00116 sockbits=bitclr(sockbits,i);}
00117 i++; }
00118 goto select_again;}
00119 if (bittest(ports,server_port)) {
00120 client_len = sizeof (struct sockaddr_in);
00121 snew = accept (server_port, &client_addr, &client_len);
00122 if (snew>0) sockbits=bitset(sockbits, snew);
00123 ports=bitclr(ports,server_port);
00124 fprintf(stderr, "new connection %d is accepted \n", snew);
00125
00126 stat--;}
00127 if (bittest(ports, 0)) {
00128 int size;
00129 size=read(0,buf,65536);
00130 if (snew) write(snew,buf,size);}
00131 if (bittest(ports, snew)) {
00132 int size;
00133 size=read(snew,buf,65536);
00134 if (snew) write(1,buf,size);}
00135 }
00136 }
00137 }