source: trunk/minix/test/select/test05_srv.c@ 9

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

Minix 3.1.2a

  • Property svn:executable set to *
File size: 5.3 KB
Line 
1/*
2 * Test name: test05_srv.c
3 *
4 * Objective: Test an impatient UDP server with timeouts
5 *
6 * Description: Implements an echo server using the UDP protocol. It is
7 * based on test04_srv, but it has a timeout value.
8 */
9
10#include <sys/types.h>
11#include <sys/ioctl.h>
12#include <sys/select.h>
13#include <sys/asynchio.h>
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#include <stdarg.h>
18#include <fcntl.h>
19#include <errno.h>
20#include <unistd.h>
21#include <net/netlib.h>
22#include <net/gen/netdb.h>
23#include <net/gen/in.h>
24#include <net/gen/udp.h>
25#include <net/gen/udp_hdr.h>
26#include <net/gen/udp_io.h>
27#include <net/hton.h>
28
29#define PORT 6000L
30
31/* type for received data */
32typedef struct
33{
34 udp_io_hdr_t header;
35 char data[1024];
36} udp_buffer_t;
37
38int udp_conf(long port) {
39
40 char *udp_device;
41 int netfd;
42 nwio_udpopt_t udpopt;
43
44 /* Get default UDP device */
45 if ((udp_device = getenv("UDP_DEVICE")) == NULL)
46 udp_device = UDP_DEVICE;
47
48 /* Open UDP connection */
49 if ((netfd = open(udp_device, O_RDWR)) < 0)
50 {
51 fprintf(stderr,"Error opening UDP connection\n");
52 return -1;
53 }
54
55 /* Configure UDP connection */
56 udpopt.nwuo_flags = NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC | NWUO_DI_BROAD
57 | NWUO_RP_ANY | NWUO_RA_ANY | NWUO_RWDATALL | NWUO_DI_IPOPT;
58
59 udpopt.nwuo_locport = (udpport_t) htons(port);
60
61 if ((ioctl(netfd, NWIOSUDPOPT, &udpopt))<0)
62 {
63 fprintf(stderr,"Error configuring the connection\n");
64 close(netfd);
65 return -1;
66 }
67
68 /* Get conf options */
69 if ((ioctl(netfd, NWIOGUDPOPT, &udpopt))<0)
70 {
71 fprintf(stderr,"Error getting the conf\n");
72 close(netfd);
73 return -1;
74 }
75
76 return netfd;
77}
78
79int main(int argc,char *argv[]) {
80 int fd;
81 ssize_t data_read;
82 udp_buffer_t buffer;
83 ipaddr_t tmp_addr;
84 udpport_t tmp_port;
85 int ret;
86 fd_set fds_read;
87 struct timeval timeout;
88 ipaddr_t client_addr, this_addr;
89 udpport_t client_port;
90
91 if ((fd = udp_conf(PORT)) < 0) {
92 fprintf(stderr, "Error configuring UDP connection\n");
93 exit(-1);
94 }
95 printf("Waiting for messages on port: %ld\n", PORT);
96 fflush(stdout);
97
98 /* get a first message so we know who is the client and we can harass it
99 afterwards */
100
101 /* Initialize fd_set */
102 FD_ZERO(&fds_read);
103 FD_SET(fd, &fds_read);
104 /* Set timeout structure */
105 timeout.tv_sec = 3;
106 timeout.tv_usec = 0;
107 ret = select(4, &fds_read, NULL, NULL, NULL);
108
109 if (ret < 0) {
110 fprintf(stderr, "Error in select\n");
111 exit(-1);
112 }
113 if (!FD_ISSET(fd, &fds_read)) {
114 fprintf(stderr, "Error: Should be receiving some data from network(?)\n");
115 exit(-1);
116 }
117 printf("Ready to receive...\n");
118 /* Read received data */
119 data_read = read(fd, &buffer, sizeof(udp_buffer_t));
120 printf("Received data: %s\n", buffer.data);
121
122 /* Can exit if the received string == exit */
123 if (!strcmp(buffer.data,"exit"))
124 exit(0);
125
126 /* Send data back, swap addresses */
127 tmp_addr = buffer.header.uih_src_addr;
128 buffer.header.uih_src_addr = buffer.header.uih_dst_addr;
129 buffer.header.uih_dst_addr = tmp_addr;
130 /* save address of both ends */
131 client_addr = tmp_addr;
132 this_addr = buffer.header.uih_src_addr;
133
134 /* Swap ports */
135 tmp_port = buffer.header.uih_src_port;
136 buffer.header.uih_src_port = buffer.header.uih_dst_port;
137 buffer.header.uih_dst_port = tmp_port;
138 /* save client port */
139 client_port = tmp_port;
140
141 /* Write the same back */
142 write(fd, &buffer, data_read);
143
144 while (1)
145 {
146
147 /* Initialize fd_set */
148 FD_ZERO(&fds_read);
149 FD_SET(fd, &fds_read);
150 /* Set timeout structure */
151 timeout.tv_sec = 3;
152 timeout.tv_usec = 0;
153 /* Wait for data available to be read (timeout) */
154 ret = select(4, &fds_read, NULL, NULL, &timeout);
155 if (ret < 0) {
156 fprintf(stderr, "Error on select: %d", errno);
157 exit(-1);
158 }
159 /* if timeout */
160 if (ret == 0) {
161 /* Send angry msg to client asking for more */
162 printf("Tired of waiting, send client an angry message\n");
163 buffer.header.uih_src_addr = this_addr;
164 buffer.header.uih_dst_addr = client_addr;
165 buffer.header.uih_src_port = PORT;
166 buffer.header.uih_dst_port = client_port;
167 strcpy(buffer.data, "Hey! I want to receive some data!\n");
168 write(fd, &buffer, sizeof(udp_buffer_t));
169 }
170 /* If receive data from network */
171 if (FD_ISSET(fd, &fds_read)) {
172 printf("Ready to receive...\n");
173 /* Read received data */
174 data_read = read(fd, &buffer, sizeof(udp_buffer_t));
175 printf("Received data: %s\n", buffer.data);
176
177 /* Can exit if the received string == exit */
178 if (!strcmp(buffer.data,"exit"))
179 break;
180
181 /* Send data back, swap addresses */
182 tmp_addr = buffer.header.uih_src_addr;
183 buffer.header.uih_src_addr = buffer.header.uih_dst_addr;
184 buffer.header.uih_dst_addr = tmp_addr;
185
186 /* Swap ports */
187 tmp_port = buffer.header.uih_src_port;
188 buffer.header.uih_src_port = buffer.header.uih_dst_port;
189 buffer.header.uih_dst_port = tmp_port;
190
191 /* Write the same back */
192 write(fd, &buffer, data_read);
193 }
194 }
195 close(fd);
196}
Note: See TracBrowser for help on using the repository browser.