kernel: print spurious interrupt message with increasing interval.
[minix.git] / test / select / test08_srv.c
blob6074035a8919cf66dec6381d46024e827751415d
1 /*
2 * Test name: test08_srv.c
4 * Objective: Test a simple TCP server waiting for urgent data.
6 * Description: Implements a echo TCP server as in test06_srv but waits
7 * for urgent data, using select on exception.
8 *
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 tcpcl;
38 nwio_tcpopt_t tcpopt;
40 /* Get default UDP device */
41 if ((tcp_device = getenv("TCP_DEVICE")) == NULL)
42 tcp_device = TCP_DEVICE;
44 /* Open TCP connection */
45 if ((netfd = open(tcp_device, O_RDWR)) < 0)
47 fprintf(stderr,"Error opening TCP connection\n");
48 return -1;
51 /* Configure TCP connection */
52 tcpconf.nwtc_flags = NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
53 tcpconf.nwtc_locport = (tcpport_t) htons(port);
55 if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0)
57 fprintf(stderr,"Error configuring the connection\n");
58 close(netfd);
59 return -1;
62 /* Get communication conf*/
63 if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
64 fprintf(stderr, "Error getting configuration\n");
65 close(netfd);
66 return -1;
69 /* Set comm options */
70 tcpopt.nwto_flags = NWTO_RCV_URG;
72 if ((ioctl(netfd, NWIOSTCPOPT, &tcpopt))<0)
74 fprintf(stderr,"Error configuring the connection\n");
75 close(netfd);
76 return -1;
79 /* Get communication opt*/
80 if ((ioctl(netfd, NWIOGTCPOPT, &tcpopt)) < 0) {
81 fprintf(stderr, "Error getting options\n");
82 close(netfd);
83 return -1;
86 /* Set conn options */
87 tcpcl.nwtcl_flags = 0;
88 printf("Waiting for connections...\n");
89 while ((ioctl(netfd, NWIOTCPLISTEN, &tcpcl)) == -1)
91 if (errno != EAGAIN)
93 fprintf(stderr,"Unable to listen for connections\n");
94 close(netfd);
96 sleep(-1);
98 return netfd;
101 int main(int argc,char *argv[]) {
102 int fd;
103 ssize_t data_read;
104 char buffer[1024];
105 int ret;
106 fd_set fds_excep;
108 if ((fd = listen(PORT)) < 0) {
109 exit(-1);
111 printf("Waiting for messages on port: %ld\n", PORT);
112 fflush(stdout);
113 /* Initialize fd_set */
114 FD_ZERO(&fds_excep);
115 FD_SET(fd, &fds_excep);
117 while (1)
119 /* Wait for data available to be read (no timeout) */
121 ret = select(4, NULL, NULL, &fds_excep, NULL);
122 if (ret < 0) {
123 fprintf(stderr, "Error on select: %d\n", errno);
124 exit(-1);
126 if (!FD_ISSET(fd, &fds_excep)) {
127 printf("Error: no URG data received (?)\n");
128 exit(-1);
131 printf("Ready to receive...\n");
132 /* Read received data */
133 data_read = read(fd, &buffer, 1024);
134 printf("Received data: %s\n", buffer);
136 /* Can exit if the received string == exit */
137 if (!strcmp(buffer,"exit"))
138 break;
140 /* Write the same back */
141 write(fd, &buffer, data_read);
143 printf("Connection finished\n");
144 close(fd);