sockstd.c
Go to the documentation of this file.
1 /****************************************************************/
2 /* sockstd.c
3 /* TCP socket <---> stdio bridge
4 /* % sockstd port <debug>
5 /* Sockstd waits for a TCP connection at 'port`.
6 /* After the connection is established, incoming data at the port
7 /* are copied to sockstd's stdout.
8 /* Input to sockstd's stdin (usually key inputs) are transferred to
9 /* the port.
10 /* Thus, pipe oriented programs, such as 'wc', 'cat', etc., will be
11 /* able to work on socket connection using the sockstd.
12 /*
13 /* (c) 1995, Toshihiro Matsui, Electrotechnical Laboraotory
14 /****************************************************************
15 
16 #include <stdio.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <netinet/in.h>
22 #include <time.h>
23 
24 #define bitset(v,i) ((v) | (1<<(i)))
25 #define bitclr(v,i) ((v) & ~(1<<(i)))
26 #define bittest(v,i) (v & (1<<(i)))
27 
28 
29 extern int make_socket_port();
30 extern void exit();
31 extern int errno;
32 
33 long transaction_count=0;
34 long debug=0;
35 
36 struct sockaddr_in *make_socket_address(port_no)
37 int port_no;
38 { int stat;
39  struct sockaddr_in *serverAddr; /* server's address */
40 
41  serverAddr=(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
42  /*bzero (serverAddr, sizeof (*serverAddr)); */
43  memset(serverAddr, 0, sizeof (*serverAddr));
44  serverAddr->sin_family = AF_INET;
45  serverAddr->sin_port = htons(port_no);
46  serverAddr->sin_addr.s_addr = htonl(INADDR_ANY);
47  return(serverAddr);
48  }
49 
51 int port;
52 { struct sockaddr_in *serverAddr;
53  int serverSock, stat;
54 
55  serverAddr= make_socket_address(port);
56  serverSock = socket (AF_INET, SOCK_STREAM, 0);
57  if (serverSock < 0) {
58  fprintf(stderr, "can't create socket\n"); exit (1);}
59  stat=bind (serverSock, serverAddr, sizeof(struct sockaddr_in));
60  if (stat<0){
61  printf ("bind failed, errno = %d\n", errno);
62  exit(1);}
63  if (listen (serverSock, 2) <0 ) {
64  printf ("listen failed\n");
65  close (serverSock);
66  exit (1);}
67  printf("listening on port= %d sock=%d\n", port, serverSock);
68  return(serverSock);}
69 
70 
71 main (argc,argv)
72 int argc;
73 char *argv[];
74 { struct sigaction sigabort;
75  struct sockaddr_in *server_addr, client_addr;
76  int server_port;
77  int port_no;
78  long ports, exceptions, sockbits;
79  struct timeval time_out;
80  char buf[65536];
81  int snew=0;
82  int i;
83  int stat;
84  long client_len;
85 
86  if (argc>1) sscanf(argv[1],"%d",&port_no);
87  else port_no=5000;
88  if (argc>2) sscanf(argv[2],"%d",&debug);
89 
90  signal(SIGINT, exit);
91  signal(SIGPIPE, SIG_IGN);
92 
93  /* Listen, for clients to connect to us. */
94  server_port=make_socket_port(port_no);
95 
96 
97  sockbits=bitset(1, server_port);
98  time_out.tv_sec=10;
99  time_out.tv_usec=0;
100 
101  while (1) {
102  select_again:
103  ports=sockbits; exceptions=sockbits;
104  stat=select(32, &ports,0, &exceptions, &time_out);
105  if (stat>0) {
106  transaction_count++;
107  if (debug>2)
108  fprintf(stderr, "select: %d count=%d ready=%x excpt=%x \n",
109  transaction_count, stat, ports, exceptions);
110  if (exceptions) {
111  /*assumes exception is delivered because of pipe_broken */
112  i=0;
113  while (i<32) {
114  if (bittest(exceptions,i)) {
115  fprintf(stderr, "exception from %d\n", i);
116  sockbits=bitclr(sockbits,i);}
117  i++; }
118  goto select_again;}
119  if (bittest(ports,server_port)) { /*new connection request*/
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);
125  /* if (connection_request) (*connection_request)(snew); */
126  stat--;}
127  if (bittest(ports, 0)) { /* stdin*/
128  int size;
129  size=read(0,buf,65536);
130  if (snew) write(snew,buf,size);}
131  if (bittest(ports, snew)) {
132  int size;
133  size=read(snew,buf,65536);
134  if (snew) write(1,buf,size);}
135  }
136  }
137  }
buf
static char buf[CHAR_SIZE]
Definition: helpsub.c:23
main
main(int argc, argv)
Definition: sockstd.c:71
memset
memset(serverAddr, 0, sizeof(*serverAddr))
serverAddr
serverAddr
Definition: sockstd.c:41
errno
int errno
argc
static int argc
Definition: transargv.c:56
make_socket_port
int make_socket_port(int port)
Definition: sockstd.c:50


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43