sockstd.c
Go to the documentation of this file.
00001 /****************************************************************/
00002 /* sockstd.c
00003 /*   TCP socket <---> stdio bridge
00004 /*   % sockstd port <debug>
00005 /*   Sockstd waits for a TCP connection at 'port`.
00006 /*   After the connection is established, incoming data at the port
00007 /*   are copied to sockstd's stdout.
00008 /*   Input to sockstd's stdin (usually key inputs) are transferred to
00009 /*   the port.
00010 /*   Thus, pipe oriented programs, such as 'wc', 'cat', etc., will be
00011 /*   able to work on socket connection using the sockstd.
00012 /*
00013 /*   (c) 1995, Toshihiro Matsui, Electrotechnical Laboraotory
00014 /****************************************************************
00015 
00016 #include <stdio.h>
00017 #include <signal.h>
00018 #include <sys/types.h>
00019 #include <sys/socket.h>
00020 #include <sys/un.h>
00021 #include <netinet/in.h>
00022 #include <time.h>
00023 
00024 #define bitset(v,i) ((v) | (1<<(i)))
00025 #define bitclr(v,i) ((v) & ~(1<<(i)))
00026 #define bittest(v,i) (v & (1<<(i)))
00027 
00028 
00029 extern int make_socket_port();
00030 extern void exit();
00031 extern int errno;
00032 
00033 long transaction_count=0;
00034 long debug=0;
00035 
00036 struct sockaddr_in *make_socket_address(port_no)
00037 int port_no;
00038 { int stat;
00039   struct sockaddr_in  *serverAddr;     /* server's address */
00040 
00041   serverAddr=(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
00042   /*bzero (serverAddr, sizeof (*serverAddr)); */
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   /* Listen, for clients to connect to us. */
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           /*assumes exception is delivered because of pipe_broken */
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)) { /*new connection request*/
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           /* if (connection_request) (*connection_request)(snew); */
00126           stat--;}
00127         if (bittest(ports, 0)) { /* stdin*/
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   }


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 6 2019 18:05:53