kernel: print spurious interrupt message with increasing interval.
[minix.git] / test / select / test06_srv.c
blobaf0459f40f1313d708b83962acb1ee90b1124766
1 /*
2 * Test name: test06_srv.c
4 * Objective: Test a simple TCP server
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 */
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>
30 #define PORT 6060L
32 int listen(long port) {
34 char *tcp_device;
35 int netfd;
36 nwio_tcpconf_t tcpconf;
37 nwio_tcpcl_t tcplistenopt;
39 /* Get default UDP device */
40 if ((tcp_device = getenv("TCP_DEVICE")) == NULL)
41 tcp_device = TCP_DEVICE;
43 /* Open TCP connection */
44 if ((netfd = open(tcp_device, O_RDWR)) < 0)
46 fprintf(stderr,"Error opening TCP connection\n");
47 return -1;
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);
54 if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0)
56 fprintf(stderr,"Error configuring the connection\n");
57 close(netfd);
58 return -1;
61 /* Get communication options */
62 if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
63 fprintf(stderr, "Error getting configuration\n");
64 close(netfd);
65 return -1;
68 /* Set conf options */
69 tcplistenopt.nwtcl_flags = 0;
70 printf("Waiting for connections...\n");
71 while ((ioctl(netfd, NWIOTCPLISTEN, &tcplistenopt)) == -1)
73 if (errno != EAGAIN)
75 fprintf(stderr,"Unable to listen for connections\n");
76 close(netfd);
78 sleep(-1);
80 return netfd;
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;
90 if ((fd = listen(PORT)) < 0) {
91 exit(-1);
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);
99 while (1)
101 /* Wait for data available to be read (no timeout) */
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);
108 if (!FD_ISSET(fd, &fds_read)) {
109 printf("Error: network fd is not ready (?)\n");
110 exit(-1);
113 printf("Ready to receive...\n");
114 /* Read received data */
115 data_read = read(fd, &buffer, 1024);
116 printf("Received data: %s\n", buffer);
118 /* Can exit if the received string == exit */
119 if (!strcmp(buffer,"exit"))
120 break;
122 /* Write the same back */
123 write(fd, &buffer, data_read);
125 printf("Connection finished\n");
126 close(fd);