|
|
1.1 root 1: /* @(#)tcpd.c 1.1 88/10/12 NFS Rev 2 Testsuite */
2: /*
3: * server for simple tcp ping program.
4: * listens on socket for connection request, then echos back
5: * request from client.
6: */
7: #define TCP_PORT 3456
8:
9: #include <stdio.h>
10: #include <sys/types.h>
11: #include <sys/socket.h>
12: #include <netinet/in.h>
13: #include <netdb.h>
14:
15: main(argc, argv)
16: char **argv;
17: {
18: int s, ns; /* sockets */
19: struct sockaddr_in addr; /* socket address */
20: int ret;
21: int addrlen = sizeof(struct sockaddr_in);
22: struct hostent *hp;
23: char buf[BUFSIZ];
24:
25: if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
26: xxit("socket");
27:
28: addr.sin_family = AF_INET;
29: addr.sin_port = htons(TCP_PORT);
30: addr.sin_addr.s_addr = INADDR_ANY;
31:
32: if (bind(s, &addr, addrlen) < 0)
33: xxit("bind");
34:
35: if (listen(s, 5) < 0)
36: xxit("listen");
37:
38: while(1) {
39: fprintf(stderr, "%s awaiting accept\n", argv[0]);
40: addrlen = sizeof(struct sockaddr_in);
41: if ((ns = accept(s, &addr, &addrlen)) < 0)
42: xxit("accept");
43: if (hp = gethostbyaddr(&addr.sin_addr, sizeof(addr.sin_addr),
44: AF_INET))
45: fprintf(stderr, "\
46: %s: accepted connection from host %s\n", argv[0], hp->h_name);
47: else
48: fprintf(stderr, "\
49: %s: accepted connection from host %x\n", argv[0], addr.sin_addr.s_addr);
50:
51: ret = read(ns, buf, BUFSIZ);
52: fprintf(stderr, " read ret %d\n", ret);
53: if (ret > 0) {
54: *buf = *buf + 1;
55: ret = write(ns, buf, ret);
56: fprintf(stderr, " write ret %d\n", ret);
57: }
58: sleep(5);
59: close(ns);
60: }
61: }
62:
63: xxit(s)
64: char *s;
65: {
66: perror(s);
67: exit(1);
68: }
69:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.