1 | /*
|
---|
2 | * Test name: test06_cli.c
|
---|
3 | *
|
---|
4 | * Objective: Test a simple TCP client
|
---|
5 | *
|
---|
6 | * Description: Implements a simple echo client using the TCP 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/tcp.h>
|
---|
24 | #include <net/gen/tcp_io.h>
|
---|
25 | #include <net/hton.h>
|
---|
26 |
|
---|
27 | #define PORT 6060L
|
---|
28 |
|
---|
29 | int tcp_connect(char *host, long port)
|
---|
30 | {
|
---|
31 | /* creates a tcp connection with specified host and port */
|
---|
32 | char *tcp_device;
|
---|
33 | struct hostent *hp;
|
---|
34 | int netfd;
|
---|
35 | nwio_tcpcl_t tcpcopt;
|
---|
36 | nwio_tcpconf_t tcpconf;
|
---|
37 | ipaddr_t dirhost;
|
---|
38 | int tries;
|
---|
39 | int result;
|
---|
40 |
|
---|
41 | /* get host address */
|
---|
42 | if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
|
---|
43 | {
|
---|
44 | fprintf(stderr,"Unknown host\n");
|
---|
45 | return(-1);
|
---|
46 | }
|
---|
47 | memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
|
---|
48 |
|
---|
49 | /* Get default TCP device */
|
---|
50 | if (( tcp_device = getenv("TCP_DEVICE") ) == NULL)
|
---|
51 | tcp_device = TCP_DEVICE;
|
---|
52 |
|
---|
53 | /* Establish TCP connection */
|
---|
54 | if ((netfd = open(tcp_device, O_RDWR)) < 0)
|
---|
55 | {
|
---|
56 | fprintf(stderr,"Error opening TCP device\n");
|
---|
57 | return -1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* Configure TCP connection */
|
---|
61 | tcpconf.nwtc_flags=NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
|
---|
62 | tcpconf.nwtc_remaddr = dirhost;
|
---|
63 | tcpconf.nwtc_remport = (tcpport_t) htons(port);
|
---|
64 |
|
---|
65 | if ((result = ioctl(netfd, NWIOSTCPCONF, &tcpconf) ) <0)
|
---|
66 | {
|
---|
67 | fprintf(stderr, "Error establishing communication\n");
|
---|
68 | printf("Error: %d\n",result);
|
---|
69 | close(netfd);
|
---|
70 | return -1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /* Get configuration for TCP comm */
|
---|
74 | if ((result = ioctl(netfd, NWIOGTCPCONF, &tcpconf) ) < 0)
|
---|
75 | {
|
---|
76 | fprintf(stderr,"Error getting configuration\n");
|
---|
77 | printf("Error: %d\n", result);
|
---|
78 | close(netfd);
|
---|
79 | return -1;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* Establish connection */
|
---|
83 | tcpcopt.nwtcl_flags = 0;
|
---|
84 | tries = 0;
|
---|
85 | while (tries < 10) {
|
---|
86 | if ( (result = ioctl(netfd, NWIOTCPCONN, &tcpcopt)) < 0 ) {
|
---|
87 | if (errno != EAGAIN)
|
---|
88 | {
|
---|
89 | fprintf(stderr, "Server is not listening\n");
|
---|
90 | close(netfd);
|
---|
91 | return(-1);
|
---|
92 | }
|
---|
93 | fprintf(stderr, "Unable to connect\n");
|
---|
94 | sleep(1);
|
---|
95 | tries++;
|
---|
96 | }
|
---|
97 | else
|
---|
98 | break; /* Connection */
|
---|
99 | }
|
---|
100 | /* Check result value */
|
---|
101 | if (result < 0) {
|
---|
102 | fprintf(stderr, "Error connecting\n");
|
---|
103 | fprintf(stderr, "Error: %d\n", result);
|
---|
104 | printf("Number of tries: %d\n", tries);
|
---|
105 | printf("Error: %d\n", errno);
|
---|
106 | close(netfd);
|
---|
107 | return -1;
|
---|
108 | }
|
---|
109 | return netfd;
|
---|
110 | }
|
---|
111 |
|
---|
112 | int main(int argc,char *argv[]) {
|
---|
113 | int fd;
|
---|
114 | ssize_t data_read;
|
---|
115 | char send_buf[1024];
|
---|
116 | char recv_buf[1024];
|
---|
117 | fd_set fds_write;
|
---|
118 | int ret;
|
---|
119 |
|
---|
120 | /* Check parameters */
|
---|
121 | if (argc !=2) {
|
---|
122 | fprintf(stderr,"Usage: %s host\n", argv[0]);
|
---|
123 | exit(-1);
|
---|
124 | }
|
---|
125 |
|
---|
126 | if ((fd = tcp_connect(argv[1], PORT) ) < 0)
|
---|
127 | exit(-1);
|
---|
128 | printf("Connected to server\n");
|
---|
129 | /* init fd_set */
|
---|
130 | FD_ZERO(&fds_write);
|
---|
131 | FD_SET(fd, &fds_write);
|
---|
132 | while (1)
|
---|
133 | {
|
---|
134 | /* Wait until it is possible to write with select */
|
---|
135 | ret = select(4, NULL, &fds_write, NULL, NULL);
|
---|
136 | if (ret < 0) {
|
---|
137 | fprintf(stderr, "Error on select waiting for write: %d\n", errno);
|
---|
138 | exit(-1);
|
---|
139 | }
|
---|
140 | if (!FD_ISSET(fd, &fds_write)) {
|
---|
141 | fprintf(stderr, "Error: The net connection is not ready for writing (?)\n");
|
---|
142 | exit(-1);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* Get a string and send it */
|
---|
146 | printf("Ready to write...\n");
|
---|
147 | printf("Send data: ");
|
---|
148 | gets(send_buf);
|
---|
149 | write(fd, &send_buf, strlen(send_buf)+1);
|
---|
150 |
|
---|
151 | /* If data sent is exit then break */
|
---|
152 | if (!strcmp(send_buf,"exit"))
|
---|
153 | break;
|
---|
154 |
|
---|
155 | /* Get server response */
|
---|
156 | data_read = read(fd, &recv_buf, 1024);
|
---|
157 | printf("Received: %s\n\n", recv_buf);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* Close TCP communication */
|
---|
161 | close(fd);
|
---|
162 | }
|
---|