source: trunk/minix/test/select/test04_cli.c@ 10

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

Minix 3.1.2a

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1/*
2 * Test name: test04_cli.c
3 *
4 * Objective: Test a simple UDP client
5 *
6 * Description: Implements a simple echo client using the UDP protocol. First
7 * it waits until it is possible to write (which is always).
8 */
9
10#include <sys/types.h>
11#include <sys/ioctl.h>
12#include <sys/select.h>
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16#include <stdarg.h>
17#include <fcntl.h>
18#include <errno.h>
19#include <unistd.h>
20#include <net/netlib.h>
21#include <net/gen/netdb.h>
22#include <net/gen/in.h>
23#include <net/gen/udp.h>
24#include <net/gen/udp_hdr.h>
25#include <net/gen/udp_io.h>
26#include <net/hton.h>
27
28#define PORT 6000L
29
30/* Type for received data */
31typedef struct
32{
33 udp_io_hdr_t header;
34 char data[1024];
35} udp_buffer_t;
36
37int udp_conf(char *host, long port, udp_io_hdr_t *header)
38{
39 /* configures UDP connection */
40 char *udp_device;
41 struct hostent *hp;
42 int netfd;
43 nwio_udpopt_t udpopt;
44 ipaddr_t dirhost;
45 int result;
46
47 /* get host address */
48 if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
49 {
50 fprintf(stderr,"Unknown host\n");
51 return(-1);
52 }
53 memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
54
55 /* Get UDP device */
56 if (( udp_device = getenv("UDP_DEVICE") ) == NULL)
57 udp_device = UDP_DEVICE;
58
59 /* Get UDP connection */
60 if ((netfd = open(udp_device, O_RDWR)) < 0)
61 {
62 fprintf(stderr,"Error opening UDP device\n");
63 return -1;
64 }
65
66 /* Configure UDP connection */
67 udpopt.nwuo_flags = NWUO_COPY | NWUO_LP_SEL | NWUO_EN_LOC | NWUO_DI_BROAD
68 | NWUO_RP_SET | NWUO_RA_SET | NWUO_RWDATALL | NWUO_DI_IPOPT;
69 udpopt.nwuo_remaddr = dirhost;
70 udpopt.nwuo_remport = (udpport_t) htons(port);
71
72 if ((result = ioctl(netfd, NWIOSUDPOPT, &udpopt) ) <0)
73 {
74 fprintf(stderr, "Error establishing communication\n");
75 printf("Error: %d\n",result);
76 close(netfd);
77 return -1;
78 }
79
80 /* Get configuration for UDP comm */
81 if ((result = ioctl(netfd, NWIOGUDPOPT, &udpopt) ) < 0)
82 {
83 fprintf(stderr,"Error getting configuration\n");
84 printf("Error: %d\n", result);
85 close(netfd);
86 return -1;
87 }
88
89 header->uih_src_addr = udpopt.nwuo_locaddr;
90 header->uih_dst_addr = udpopt.nwuo_remaddr;
91 header->uih_src_port = udpopt.nwuo_locport;
92 header->uih_dst_port = udpopt.nwuo_remport;
93
94 return netfd;
95}
96
97int main(int argc,char *argv[]) {
98 int fd;
99 ssize_t data_read;
100 udp_buffer_t buffer_send, buffer_rec;
101 fd_set fds_write;
102 int ret;
103
104 /* Check parameters */
105 if (argc !=2) {
106 fprintf(stderr,"Usage: %s host\n", argv[0]);
107 exit(-1);
108 }
109
110 if ((fd = udp_conf(argv[1], PORT, &buffer_send.header) ) < 0)
111 exit(-1);
112
113 /* init fd_set */
114 FD_ZERO(&fds_write);
115 FD_SET(fd, &fds_write);
116
117 while (1)
118 {
119 /* Wait until it is possible to write with select */
120
121 ret = select(4, NULL, &fds_write, NULL, NULL);
122 if (ret < 0) {
123 fprintf(stderr, "Error on select waiting for write: %d\n", errno);
124 exit(-1);
125 }
126 if (!FD_ISSET(fd, &fds_write)) {
127 fprintf(stderr, "Error: The net connection is not ready for writing (?)\n");
128 exit(-1);
129 }
130
131 /* Get a string and send it */
132 printf("Ready to write...\n");
133 printf("Send data: ");
134 gets(buffer_send.data);
135 write(fd, &buffer_send, sizeof(udp_buffer_t));
136
137 /* If data sent is exit then break */
138 if (!strcmp(buffer_send.data,"exit"))
139 break;
140
141 /* Get server response */
142 data_read = read(fd, &buffer_rec, sizeof(udp_buffer_t));
143 printf("Received: %s\n\n", buffer_rec.data);
144 }
145
146 /* Close UDP communication */
147 close(fd);
148}
Note: See TracBrowser for help on using the repository browser.