tar: use utime() to restore timestamps
[minix.git] / test / select / test07_cli.c
blob8bc21010a3cc3a27dcec2e66460319ae938479b2
1 /*
2 * Test name: test07_cli.c
4 * Objective: Test an impatient TCP client with timeout which waits input both
5 * from the terminal and from the network connection.
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.
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>
31 #define PORT 6060L
33 int tcp_connect(char *host, long port)
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;
45 /* get host address */
46 if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
48 fprintf(stderr,"Unknown host\n");
49 return(-1);
51 memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
53 /* Get default TCP device */
54 if (( tcp_device = getenv("TCP_DEVICE") ) == NULL)
55 tcp_device = TCP_DEVICE;
57 /* Establish TCP connection */
58 if ((netfd = open(tcp_device, O_RDWR)) < 0)
60 fprintf(stderr,"Error opening TCP device\n");
61 return -1;
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);
69 if ((result = ioctl(netfd, NWIOSTCPCONF, &tcpconf) ) <0)
71 fprintf(stderr, "Error establishing communication\n");
72 printf("Error: %d\n",result);
73 close(netfd);
74 return -1;
77 /* Get configuration for TCP comm */
78 if ((result = ioctl(netfd, NWIOGTCPCONF, &tcpconf) ) < 0)
80 fprintf(stderr,"Error getting configuration\n");
81 printf("Error: %d\n", result);
82 close(netfd);
83 return -1;
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)
93 fprintf(stderr, "Server is not listening\n");
94 close(netfd);
95 return(-1);
97 fprintf(stderr, "Unable to connect\n");
98 sleep(1);
99 tries++;
101 else
102 break; /* Connection */
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;
113 return netfd;
116 int 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;
125 /* Check parameters */
126 if (argc !=2) {
127 fprintf(stderr,"Usage: %s host\n", argv[0]);
128 exit(-1);
131 if ((fd = tcp_connect(argv[1], PORT) ) < 0)
132 exit(-1);
133 printf("Connected to server\n");
135 while (1)
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;
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);
154 /* timeout */
155 if (ret == 0) {
156 printf("\nClient says: Hey! I want to send some data!!\n");
157 continue;
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);
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);
170 /* If data sent is exit then break */
171 if (!strcmp(send_buf,"exit"))
172 break;
174 /* Get server response */
175 data_read = read(fd, &recv_buf, 1024);
176 printf("Received: %s\n\n", recv_buf);
180 /* Close TCP communication */
181 close(fd);