source: trunk/minix/test/select/test04_srv.c@ 22

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

Minix 3.1.2a

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1/*
2 * Test name: test04_srv.c
3 *
4 * Objective: Test a simple UDP server
5 *
6 * Description: Implements a simple echo server using the UDP 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 <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
88 if ((fd = udp_conf(PORT)) < 0) {
89 fprintf(stderr, "Error configuring UDP connection\n");
90 exit(-1);
91 }
92 printf("Waiting for messages on port: %ld\n", PORT);
93 fflush(stdout);
94 /* Initialize fd_set */
95 FD_ZERO(&fds_read);
96 FD_SET(fd, &fds_read);
97
98 while (1)
99 {
100 /* Wait for data available to be read (no timeout) */
101 ret = select(4, &fds_read, NULL, NULL, NULL);
102 if (ret < 0) {
103 fprintf(stderr, "Error on select: %d", errno);
104 exit(-1);
105 }
106 if (!FD_ISSET(fd, &fds_read)) {
107 printf("Error: network fd is not ready (?)\n");
108 exit(-1);
109 }
110 printf("Ready to receive...\n");
111 /* Read received data */
112 data_read = read(fd, &buffer, sizeof(udp_buffer_t));
113 printf("Received data: %s\n", buffer.data);
114
115 /* Can exit if the received string == exit */
116 if (!strcmp(buffer.data,"exit"))
117 break;
118
119 /* Send data back, swap addresses */
120 tmp_addr = buffer.header.uih_src_addr;
121 buffer.header.uih_src_addr = buffer.header.uih_dst_addr;
122 buffer.header.uih_dst_addr = tmp_addr;
123
124 /* Swap ports */
125 tmp_port = buffer.header.uih_src_port;
126 buffer.header.uih_src_port = buffer.header.uih_dst_port;
127 buffer.header.uih_dst_port = tmp_port;
128
129 /* Write the same back */
130 write(fd, &buffer, data_read);
131 }
132
133 close(fd);
134}
Note: See TracBrowser for help on using the repository browser.