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>
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>
33 /* Type for received data */
40 int udp_conf(char *host
, long port
, udp_io_hdr_t
*header
)
42 /* configures UDP connection */
50 /* get host address */
51 if ((hp
= gethostbyname(host
)) == (struct hostent
*) NULL
)
53 fprintf(stderr
,"Unknown host\n");
56 memcpy((char *)&dirhost
, (char *)hp
->h_addr
, hp
->h_length
);
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");
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
);
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
);
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
;
100 int main(int argc
,char *argv
[]) {
103 udp_buffer_t buffer_send
, buffer_rec
;
106 struct timeval timeout
;
108 /* Check parameters */
110 fprintf(stderr
,"Usage: %s host\n", argv
[0]);
114 if ((fd
= udp_conf(argv
[1], PORT
, &buffer_send
.header
) ) < 0)
122 FD_SET(0, &fds_read
);
123 FD_SET(fd
, &fds_read
);
128 printf("Send data: ");
130 /* Wait until it is possible to write with select */
131 ret
= select(4, &fds_read
, NULL
, NULL
, &timeout
);
133 fprintf(stderr
, "Error on select waiting for read: %d\n", errno
);
137 printf("\nClient says: Hey! I want to send data!!\n");
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
);
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"))
156 /* Get server response */
157 data_read
= read(fd
, &buffer_rec
, sizeof(udp_buffer_t
));
158 printf("Received: %s\n\n", buffer_rec
.data
);
163 /* Close UDP communication */