[9] | 1 | /*
|
---|
| 2 | * Test name: test05_cli.c
|
---|
| 3 | *
|
---|
| 4 | * Objective: Test a impatient UDP client with timeout and incoming data from
|
---|
| 5 | * network and terminal.
|
---|
| 6 | *
|
---|
| 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.
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 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>
|
---|
| 30 |
|
---|
| 31 | #define PORT 6000L
|
---|
| 32 |
|
---|
| 33 | /* Type for received data */
|
---|
| 34 | typedef struct
|
---|
| 35 | {
|
---|
| 36 | udp_io_hdr_t header;
|
---|
| 37 | char data[1024];
|
---|
| 38 | } udp_buffer_t;
|
---|
| 39 |
|
---|
| 40 | int udp_conf(char *host, long port, udp_io_hdr_t *header)
|
---|
| 41 | {
|
---|
| 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;
|
---|
| 49 |
|
---|
| 50 | /* get host address */
|
---|
| 51 | if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
|
---|
| 52 | {
|
---|
| 53 | fprintf(stderr,"Unknown host\n");
|
---|
| 54 | return(-1);
|
---|
| 55 | }
|
---|
| 56 | memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
|
---|
| 57 |
|
---|
| 58 | /* Get UDP device */
|
---|
| 59 | if (( udp_device = getenv("UDP_DEVICE") ) == NULL)
|
---|
| 60 | udp_device = UDP_DEVICE;
|
---|
| 61 |
|
---|
| 62 | /* Get UDP connection */
|
---|
| 63 | if ((netfd = open(udp_device, O_RDWR)) < 0)
|
---|
| 64 | {
|
---|
| 65 | fprintf(stderr,"Error opening UDP device\n");
|
---|
| 66 | return -1;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 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);
|
---|
| 74 |
|
---|
| 75 | if ((result = ioctl(netfd, NWIOSUDPOPT, &udpopt) ) <0)
|
---|
| 76 | {
|
---|
| 77 | fprintf(stderr, "Error establishing communication\n");
|
---|
| 78 | printf("Error: %d\n",result);
|
---|
| 79 | close(netfd);
|
---|
| 80 | return -1;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /* Get configuration for UDP comm */
|
---|
| 84 | if ((result = ioctl(netfd, NWIOGUDPOPT, &udpopt) ) < 0)
|
---|
| 85 | {
|
---|
| 86 | fprintf(stderr,"Error getting configuration\n");
|
---|
| 87 | printf("Error: %d\n", result);
|
---|
| 88 | close(netfd);
|
---|
| 89 | return -1;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 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;
|
---|
| 96 |
|
---|
| 97 | return netfd;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 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;
|
---|
| 107 |
|
---|
| 108 | /* Check parameters */
|
---|
| 109 | if (argc !=2) {
|
---|
| 110 | fprintf(stderr,"Usage: %s host\n", argv[0]);
|
---|
| 111 | exit(-1);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if ((fd = udp_conf(argv[1], PORT, &buffer_send.header) ) < 0)
|
---|
| 115 | exit(-1);
|
---|
| 116 |
|
---|
| 117 | while (1)
|
---|
| 118 | {
|
---|
| 119 |
|
---|
| 120 | /* init fd_set */
|
---|
| 121 | FD_ZERO(&fds_read);
|
---|
| 122 | FD_SET(0, &fds_read);
|
---|
| 123 | FD_SET(fd, &fds_read);
|
---|
| 124 |
|
---|
| 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);
|
---|
| 135 | }
|
---|
| 136 | if (ret == 0) {
|
---|
| 137 | printf("\nClient says: Hey! I want to send data!!\n");
|
---|
| 138 | fflush(stdout);
|
---|
| 139 | continue;
|
---|
| 140 | }
|
---|
| 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);
|
---|
| 146 | }
|
---|
| 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));
|
---|
| 152 |
|
---|
| 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);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /* Close UDP communication */
|
---|
| 164 | close(fd);
|
---|
| 165 | }
|
---|