use oxpcie only if enabled to avoid baud bottleneck of uart.
[minix.git] / test / select / test05_cli.c
blobaad9bf0e667293aa47f15ce3fc22f58768052524
1 /*
2 * Test name: test05_cli.c
4 * Objective: Test a impatient UDP client with timeout and incoming data from
5 * network and terminal.
7 * Description: Implements a echo client using the UDP protocol. It is
8 * based on test04_cli, but the difference is that it uses timeout and waits
9 * for data both from terminal (stdin) and network connection.
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
15 #include <sys/asynchio.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <net/netlib.h>
24 #include <net/gen/netdb.h>
25 #include <net/gen/in.h>
26 #include <net/gen/udp.h>
27 #include <net/gen/udp_hdr.h>
28 #include <net/gen/udp_io.h>
29 #include <net/hton.h>
31 #define PORT 6000L
33 /* Type for received data */
34 typedef struct
36 udp_io_hdr_t header;
37 char data[1024];
38 } udp_buffer_t;
40 int udp_conf(char *host, long port, udp_io_hdr_t *header)
42 /* configures UDP connection */
43 char *udp_device;
44 struct hostent *hp;
45 int netfd;
46 nwio_udpopt_t udpopt;
47 ipaddr_t dirhost;
48 int result;
50 /* get host address */
51 if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
53 fprintf(stderr,"Unknown host\n");
54 return(-1);
56 memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
58 /* Get UDP device */
59 if (( udp_device = getenv("UDP_DEVICE") ) == NULL)
60 udp_device = UDP_DEVICE;
62 /* Get UDP connection */
63 if ((netfd = open(udp_device, O_RDWR)) < 0)
65 fprintf(stderr,"Error opening UDP device\n");
66 return -1;
69 /* Configure UDP connection */
70 udpopt.nwuo_flags = NWUO_COPY | NWUO_LP_SEL | NWUO_EN_LOC | NWUO_DI_BROAD
71 | NWUO_RP_SET | NWUO_RA_SET | NWUO_RWDATALL | NWUO_DI_IPOPT;
72 udpopt.nwuo_remaddr = dirhost;
73 udpopt.nwuo_remport = (udpport_t) htons(port);
75 if ((result = ioctl(netfd, NWIOSUDPOPT, &udpopt) ) <0)
77 fprintf(stderr, "Error establishing communication\n");
78 printf("Error: %d\n",result);
79 close(netfd);
80 return -1;
83 /* Get configuration for UDP comm */
84 if ((result = ioctl(netfd, NWIOGUDPOPT, &udpopt) ) < 0)
86 fprintf(stderr,"Error getting configuration\n");
87 printf("Error: %d\n", result);
88 close(netfd);
89 return -1;
92 header->uih_src_addr = udpopt.nwuo_locaddr;
93 header->uih_dst_addr = udpopt.nwuo_remaddr;
94 header->uih_src_port = udpopt.nwuo_locport;
95 header->uih_dst_port = udpopt.nwuo_remport;
97 return netfd;
100 int main(int argc,char *argv[]) {
101 int fd;
102 ssize_t data_read;
103 udp_buffer_t buffer_send, buffer_rec;
104 fd_set fds_read;
105 int ret;
106 struct timeval timeout;
108 /* Check parameters */
109 if (argc !=2) {
110 fprintf(stderr,"Usage: %s host\n", argv[0]);
111 exit(-1);
114 if ((fd = udp_conf(argv[1], PORT, &buffer_send.header) ) < 0)
115 exit(-1);
117 while (1)
120 /* init fd_set */
121 FD_ZERO(&fds_read);
122 FD_SET(0, &fds_read);
123 FD_SET(fd, &fds_read);
125 /* set timeval */
126 timeout.tv_sec = 5;
127 timeout.tv_usec = 0;
128 printf("Send data: ");
129 fflush(stdout);
130 /* Wait until it is possible to write with select */
131 ret = select(4, &fds_read, NULL, NULL, &timeout);
132 if (ret < 0) {
133 fprintf(stderr, "Error on select waiting for read: %d\n", errno);
134 exit(-1);
136 if (ret == 0) {
137 printf("\nClient says: Hey! I want to send data!!\n");
138 fflush(stdout);
139 continue;
141 /* if got message from server */
142 if (FD_ISSET(fd, &fds_read)) {
143 data_read = read(fd, &buffer_rec, sizeof(udp_buffer_t));
144 printf("Server says: %s\n\n", buffer_rec.data);
145 fflush(stdout);
147 /* if got data from terminal */
148 if (FD_ISSET(0, &fds_read)) {
149 /* Get a string and send it */
150 gets(buffer_send.data);
151 write(fd, &buffer_send, sizeof(udp_buffer_t));
153 /* If data sent is exit then break */
154 if (!strcmp(buffer_send.data,"exit"))
155 break;
156 /* Get server response */
157 data_read = read(fd, &buffer_rec, sizeof(udp_buffer_t));
158 printf("Received: %s\n\n", buffer_rec.data);
159 fflush(stdout);
163 /* Close UDP communication */
164 close(fd);