mkfs: symlink support
[minix.git] / test / select / test08_cli.c
blob20a8cedf101fa91afebd8540e1cc05325eebe71a
1 /*
2 * Test name: test08_cli.c
4 * Objective: Test select on urgent data. TCP client.
6 * Description: It is based on test06_cli but sends urgent data.
7 *
8 */
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>
27 #define PORT 6060L
29 int tcp_connect(char *host, long port)
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 tcpcl;
36 nwio_tcpopt_t tcpopt;
37 nwio_tcpconf_t tcpconf;
38 ipaddr_t dirhost;
39 int tries;
40 int result;
42 /* get host address */
43 if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
45 fprintf(stderr,"Unknown host\n");
46 return(-1);
48 memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
50 /* Get default TCP device */
51 if (( tcp_device = getenv("TCP_DEVICE") ) == NULL)
52 tcp_device = TCP_DEVICE;
54 /* Establish TCP connection */
55 if ((netfd = open(tcp_device, O_RDWR)) < 0)
57 fprintf(stderr,"Error opening TCP device\n");
58 return -1;
61 /* Configure TCP connection */
62 tcpconf.nwtc_flags=NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
63 tcpconf.nwtc_remaddr = dirhost;
64 tcpconf.nwtc_remport = (tcpport_t) htons(port);
66 if ((result = ioctl(netfd, NWIOSTCPCONF, &tcpconf) ) <0)
68 fprintf(stderr, "Error establishing communication\n");
69 printf("Error: %d\n",result);
70 close(netfd);
71 return -1;
74 /* Get configuration for TCP comm */
75 if ((result = ioctl(netfd, NWIOGTCPCONF, &tcpconf) ) < 0)
77 fprintf(stderr,"Error getting configuration\n");
78 printf("Error: %d\n", result);
79 close(netfd);
80 return -1;
83 /* Establish connection options (send URG data) */
84 tcpopt.nwto_flags = NWTO_SND_URG_MASK;
86 /* Set options for TCP comm */
87 if ((result = ioctl(netfd, NWIOSTCPOPT, &tcpopt) ) < 0)
89 fprintf(stderr,"Error getting configuration\n");
90 printf("Error: %d\n", result);
91 close(netfd);
92 return -1;
95 /* Get configuration for TCP comm */
96 if ((result = ioctl(netfd, NWIOGTCPOPT, &tcpopt) ) < 0)
98 fprintf(stderr,"Error getting options\n");
99 printf("Error: %d\n", result);
100 close(netfd);
101 return -1;
104 tcpcl.nwtcl_flags = 0;
105 tries = 0;
106 while (tries < 10) {
107 if ( (result = ioctl(netfd, NWIOTCPCONN, &tcpcl)) < 0 ) {
108 if (errno != EAGAIN)
110 fprintf(stderr, "Server is not listening\n");
111 close(netfd);
112 return(-1);
114 fprintf(stderr, "Unable to connect\n");
115 sleep(1);
116 tries++;
118 else
119 break; /* Connection */
121 /* Check result value */
122 if (result < 0) {
123 fprintf(stderr, "Error connecting\n");
124 fprintf(stderr, "Error: %d\n", result);
125 printf("Number of tries: %d\n", tries);
126 printf("Error: %d\n", errno);
127 close(netfd);
128 return -1;
130 return netfd;
133 int main(int argc,char *argv[]) {
134 int fd;
135 ssize_t data_read;
136 char send_buf[1024];
137 char recv_buf[1024];
138 fd_set fds_write;
139 int ret;
141 /* Check parameters */
142 if (argc !=2) {
143 fprintf(stderr,"Usage: %s host\n", argv[0]);
144 exit(-1);
147 if ((fd = tcp_connect(argv[1], PORT) ) < 0)
148 exit(-1);
149 printf("Connected to server\n");
150 /* init fd_set */
151 FD_ZERO(&fds_write);
152 FD_SET(fd, &fds_write);
153 while (1)
155 /* Wait until it is possible to write with select */
156 ret = select(4, NULL, &fds_write, NULL, NULL);
157 if (ret < 0) {
158 fprintf(stderr, "Error on select waiting for write: %d\n", errno);
159 exit(-1);
161 if (!FD_ISSET(fd, &fds_write)) {
162 fprintf(stderr, "Error: The net connection is not ready for writing (?)\n");
163 exit(-1);
166 /* Get a string and send it */
167 printf("Ready to write...\n");
168 printf("Send data: ");
169 gets(send_buf);
170 write(fd, &send_buf, strlen(send_buf)+1);
172 /* If data sent is exit then break */
173 if (!strcmp(send_buf,"exit"))
174 break;
176 /* Get server response */
177 data_read = read(fd, &recv_buf, 1024);
178 printf("Received: %s\n\n", recv_buf);
181 /* Close UDP communication */
182 close(fd);