source: trunk/minix/test/select/test08_srv.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1/*
2 * Test name: test08_srv.c
3 *
4 * Objective: Test a simple TCP server waiting for urgent data.
5 *
6 * Description: Implements a echo TCP server as in test06_srv but waits
7 * for urgent data, using select on exception.
8 *
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
32int listen(long port) {
33
34 char *tcp_device;
35 int netfd;
36 nwio_tcpconf_t tcpconf;
37 nwio_tcpcl_t tcpcl;
38 nwio_tcpopt_t tcpopt;
39
40 /* Get default UDP device */
41 if ((tcp_device = getenv("TCP_DEVICE")) == NULL)
42 tcp_device = TCP_DEVICE;
43
44 /* Open TCP connection */
45 if ((netfd = open(tcp_device, O_RDWR)) < 0)
46 {
47 fprintf(stderr,"Error opening TCP connection\n");
48 return -1;
49 }
50
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);
54
55 if ((ioctl(netfd, NWIOSTCPCONF, &tcpconf))<0)
56 {
57 fprintf(stderr,"Error configuring the connection\n");
58 close(netfd);
59 return -1;
60 }
61
62 /* Get communication conf*/
63 if ((ioctl(netfd, NWIOGTCPCONF, &tcpconf)) < 0) {
64 fprintf(stderr, "Error getting configuration\n");
65 close(netfd);
66 return -1;
67 }
68
69 /* Set comm options */
70 tcpopt.nwto_flags = NWTO_RCV_URG;
71
72 if ((ioctl(netfd, NWIOSTCPOPT, &tcpopt))<0)
73 {
74 fprintf(stderr,"Error configuring the connection\n");
75 close(netfd);
76 return -1;
77 }
78
79 /* Get communication opt*/
80 if ((ioctl(netfd, NWIOGTCPOPT, &tcpopt)) < 0) {
81 fprintf(stderr, "Error getting options\n");
82 close(netfd);
83 return -1;
84 }
85
86 /* Set conn options */
87 tcpcl.nwtcl_flags = 0;
88 printf("Waiting for connections...\n");
89 while ((ioctl(netfd, NWIOTCPLISTEN, &tcpcl)) == -1)
90 {
91 if (errno != EAGAIN)
92 {
93 fprintf(stderr,"Unable to listen for connections\n");
94 close(netfd);
95 }
96 sleep(-1);
97 }
98 return netfd;
99}
100
101int 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;
107
108 if ((fd = listen(PORT)) < 0) {
109 exit(-1);
110 }
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);
116
117 while (1)
118 {
119 /* Wait for data available to be read (no timeout) */
120
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);
125 }
126 if (!FD_ISSET(fd, &fds_excep)) {
127 printf("Error: no URG data received (?)\n");
128 exit(-1);
129 }
130
131 printf("Ready to receive...\n");
132 /* Read received data */
133 data_read = read(fd, &buffer, 1024);
134 printf("Received data: %s\n", buffer);
135
136 /* Can exit if the received string == exit */
137 if (!strcmp(buffer,"exit"))
138 break;
139
140 /* Write the same back */
141 write(fd, &buffer, data_read);
142 }
143 printf("Connection finished\n");
144 close(fd);
145}
Note: See TracBrowser for help on using the repository browser.