source: trunk/minix/test/select/test07_cli.c@ 9

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

Minix 3.1.2a

  • Property svn:executable set to *
File size: 4.3 KB
Line 
1/*
2 * Test name: test07_cli.c
3 *
4 * Objective: Test an impatient TCP client with timeout which waits input both
5 * from the terminal and from the network connection.
6 *
7 * Description: Implements a echo client using the TCP protocol with a timeout
8 * value. It waites for data on both the terminal and the network connection and
9 * prints an angry message asking for more data if there is a timeout.
10 *
11 */
12
13#include <sys/types.h>
14#include <sys/ioctl.h>
15#include <sys/select.h>
16#include <sys/asynchio.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdlib.h>
20#include <stdarg.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <unistd.h>
24#include <net/netlib.h>
25#include <net/gen/netdb.h>
26#include <net/gen/in.h>
27#include <net/gen/tcp.h>
28#include <net/gen/tcp_io.h>
29#include <net/hton.h>
30
31#define PORT 6060L
32
33int tcp_connect(char *host, long port)
34{
35 /* creates a tcp connection with specified host and port */
36 char *tcp_device;
37 struct hostent *hp;
38 int netfd;
39 nwio_tcpcl_t tcpcopt;
40 nwio_tcpconf_t tcpconf;
41 ipaddr_t dirhost;
42 int tries;
43 int result;
44
45 /* get host address */
46 if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
47 {
48 fprintf(stderr,"Unknown host\n");
49 return(-1);
50 }
51 memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
52
53 /* Get default TCP device */
54 if (( tcp_device = getenv("TCP_DEVICE") ) == NULL)
55 tcp_device = TCP_DEVICE;
56
57 /* Establish TCP connection */
58 if ((netfd = open(tcp_device, O_RDWR)) < 0)
59 {
60 fprintf(stderr,"Error opening TCP device\n");
61 return -1;
62 }
63
64 /* Configure TCP connection */
65 tcpconf.nwtc_flags=NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
66 tcpconf.nwtc_remaddr = dirhost;
67 tcpconf.nwtc_remport = (tcpport_t) htons(port);
68
69 if ((result = ioctl(netfd, NWIOSTCPCONF, &tcpconf) ) <0)
70 {
71 fprintf(stderr, "Error establishing communication\n");
72 printf("Error: %d\n",result);
73 close(netfd);
74 return -1;
75 }
76
77 /* Get configuration for TCP comm */
78 if ((result = ioctl(netfd, NWIOGTCPCONF, &tcpconf) ) < 0)
79 {
80 fprintf(stderr,"Error getting configuration\n");
81 printf("Error: %d\n", result);
82 close(netfd);
83 return -1;
84 }
85
86 /* Establish connection */
87 tcpcopt.nwtcl_flags = 0;
88 tries = 0;
89 while (tries < 10) {
90 if ( (result = ioctl(netfd, NWIOTCPCONN, &tcpcopt)) < 0 ) {
91 if (errno != EAGAIN)
92 {
93 fprintf(stderr, "Server is not listening\n");
94 close(netfd);
95 return(-1);
96 }
97 fprintf(stderr, "Unable to connect\n");
98 sleep(1);
99 tries++;
100 }
101 else
102 break; /* Connection */
103 }
104 /* Check result value */
105 if (result < 0) {
106 fprintf(stderr, "Error connecting\n");
107 fprintf(stderr, "Error: %d\n", result);
108 printf("Number of tries: %d\n", tries);
109 printf("Error: %d\n", errno);
110 close(netfd);
111 return -1;
112 }
113 return netfd;
114}
115
116int main(int argc,char *argv[]) {
117 int fd;
118 ssize_t data_read;
119 char send_buf[1024];
120 char recv_buf[1024];
121 fd_set fds_read;
122 int ret;
123 struct timeval timeout;
124
125 /* Check parameters */
126 if (argc !=2) {
127 fprintf(stderr,"Usage: %s host\n", argv[0]);
128 exit(-1);
129 }
130
131 if ((fd = tcp_connect(argv[1], PORT) ) < 0)
132 exit(-1);
133 printf("Connected to server\n");
134
135 while (1)
136 {
137
138 /* init fd_set */
139 FD_ZERO(&fds_read);
140 FD_SET(0, &fds_read); /* stdin */
141 FD_SET(fd, &fds_read);
142 /* set timeout */
143 timeout.tv_sec = 3;
144 timeout.tv_usec = 0;
145
146 printf("Send data: ");
147 fflush(stdout);
148 /* Wait until it is possible to read with select */
149 ret = select(4, &fds_read, NULL, NULL, &timeout);
150 if (ret < 0) {
151 fprintf(stderr, "Error on select waiting for write: %d\n", errno);
152 exit(-1);
153 }
154 /* timeout */
155 if (ret == 0) {
156 printf("\nClient says: Hey! I want to send some data!!\n");
157 continue;
158 }
159 /* handle data from network */
160 if (FD_ISSET(fd, &fds_read)) {
161 data_read = read(fd, &recv_buf, 1024);
162 printf("Server says: %s\n\n", recv_buf);
163 }
164 /* handle data from terminal */
165 if (FD_ISSET(0, &fds_read)) {
166 /* Get a string and send it */
167 gets(send_buf);
168 write(fd, &send_buf, strlen(send_buf)+1);
169
170 /* If data sent is exit then break */
171 if (!strcmp(send_buf,"exit"))
172 break;
173
174 /* Get server response */
175 data_read = read(fd, &recv_buf, 1024);
176 printf("Received: %s\n\n", recv_buf);
177 }
178 }
179
180 /* Close TCP communication */
181 close(fd);
182}
Note: See TracBrowser for help on using the repository browser.