1 | /*
|
---|
2 | * Test name: test06_srv.c
|
---|
3 | *
|
---|
4 | * Objective: Test a simple TCP server
|
---|
5 | *
|
---|
6 | * Description: Implements a simple echo server using the TCP protocol. Instead
|
---|
7 | * of blocking on read(), it performs a select call first blocking there
|
---|
8 | * until there is data to be read
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <sys/types.h>
|
---|
12 | #include <sys/ioctl.h>
|
---|
13 | #include <sys/select.h>
|
---|
14 | #include <sys/wait.h>
|
---|
15 | #include <stdio.h>
|
---|
16 | #include <string.h>
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include <stdarg.h>
|
---|
19 | #include <fcntl.h>
|
---|
20 | #include <errno.h>
|
---|
21 | #include <unistd.h>
|
---|
22 | #include <net/netlib.h>
|
---|
23 | #include <net/gen/netdb.h>
|
---|
24 | #include <net/gen/in.h>
|
---|
25 | #include <net/gen/tcp.h>
|
---|
26 | #include <net/gen/tcp_io.h>
|
---|
27 | #include <net/hton.h>
|
---|
28 | #include <net/gen/inet.h>
|
---|
29 |
|
---|
30 | #define PORT 6060L
|
---|
31 |
|
---|
32 | int listen(long port) {
|
---|
33 |
|
---|
34 | char *tcp_device;
|
---|
35 | int netfd;
|
---|
36 | nwio_tcpconf_t tcpconf;
|
---|
37 | nwio_tcpcl_t tcplistenopt;
|
---|
38 |
|
---|
39 | /* Get default UDP device */
|
---|
40 | if ((tcp_device = getenv("TCP_DEVICE")) == NULL)
|
---|
41 | tcp_device = TCP_DEVICE;
|
---|
42 |
|
---|
43 | /* Open TCP connection */
|
---|
44 | if ((netfd = open(tcp_device, O_RDWR)) < 0)
|
---|
45 | {
|
---|
46 | fprintf(stderr,"Error opening TCP connection\n");
|
---|
47 | return -1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /* Configure TCP connection */
|
---|
51 | tcpconf.nwtc_flags = NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
|
---|
52 | tcpconf.nwtc_locport = (tcpport_t) htons(port);
|
---|
53 |
|
---|
54 | if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0)
|
---|
55 | {
|
---|
56 | fprintf(stderr,"Error configuring the connection\n");
|
---|
57 | close(netfd);
|
---|
58 | return -1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* Get communication options */
|
---|
62 | if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
|
---|
63 | fprintf(stderr, "Error getting configuration\n");
|
---|
64 | close(netfd);
|
---|
65 | return -1;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* Set conf options */
|
---|
69 | tcplistenopt.nwtcl_flags = 0;
|
---|
70 | printf("Waiting for connections...\n");
|
---|
71 | while ((ioctl(netfd, NWIOTCPLISTEN, &tcplistenopt)) == -1)
|
---|
72 | {
|
---|
73 | if (errno != EAGAIN)
|
---|
74 | {
|
---|
75 | fprintf(stderr,"Unable to listen for connections\n");
|
---|
76 | close(netfd);
|
---|
77 | }
|
---|
78 | sleep(-1);
|
---|
79 | }
|
---|
80 | return netfd;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int main(int argc,char *argv[]) {
|
---|
84 | int fd;
|
---|
85 | ssize_t data_read;
|
---|
86 | char buffer[1024];
|
---|
87 | int ret;
|
---|
88 | fd_set fds_read;
|
---|
89 |
|
---|
90 | if ((fd = listen(PORT)) < 0) {
|
---|
91 | exit(-1);
|
---|
92 | }
|
---|
93 | printf("Waiting for messages on port: %ld\n", PORT);
|
---|
94 | fflush(stdout);
|
---|
95 | /* Initialize fd_set */
|
---|
96 | FD_ZERO(&fds_read);
|
---|
97 | FD_SET(fd, &fds_read);
|
---|
98 |
|
---|
99 | while (1)
|
---|
100 | {
|
---|
101 | /* Wait for data available to be read (no timeout) */
|
---|
102 |
|
---|
103 | ret = select(4, &fds_read, NULL, NULL, NULL);
|
---|
104 | if (ret < 0) {
|
---|
105 | fprintf(stderr, "Error on select: %d\n", errno);
|
---|
106 | exit(-1);
|
---|
107 | }
|
---|
108 | if (!FD_ISSET(fd, &fds_read)) {
|
---|
109 | printf("Error: network fd is not ready (?)\n");
|
---|
110 | exit(-1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | printf("Ready to receive...\n");
|
---|
114 | /* Read received data */
|
---|
115 | data_read = read(fd, &buffer, 1024);
|
---|
116 | printf("Received data: %s\n", buffer);
|
---|
117 |
|
---|
118 | /* Can exit if the received string == exit */
|
---|
119 | if (!strcmp(buffer,"exit"))
|
---|
120 | break;
|
---|
121 |
|
---|
122 | /* Write the same back */
|
---|
123 | write(fd, &buffer, data_read);
|
---|
124 | }
|
---|
125 | printf("Connection finished\n");
|
---|
126 | close(fd);
|
---|
127 | }
|
---|