source: trunk/minix/test/select/test08_cli.c@ 15

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

Minix 3.1.2a

  • Property svn:executable set to *
File size: 4.2 KB
RevLine 
[9]1/*
2 * Test name: test08_cli.c
3 *
4 * Objective: Test select on urgent data. TCP client.
5 *
6 * Description: It is based on test06_cli but sends urgent data.
7 *
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
29int 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 tcpcl;
36 nwio_tcpopt_t tcpopt;
37 nwio_tcpconf_t tcpconf;
38 ipaddr_t dirhost;
39 int tries;
40 int result;
41
42 /* get host address */
43 if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
44 {
45 fprintf(stderr,"Unknown host\n");
46 return(-1);
47 }
48 memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
49
50 /* Get default TCP device */
51 if (( tcp_device = getenv("TCP_DEVICE") ) == NULL)
52 tcp_device = TCP_DEVICE;
53
54 /* Establish TCP connection */
55 if ((netfd = open(tcp_device, O_RDWR)) < 0)
56 {
57 fprintf(stderr,"Error opening TCP device\n");
58 return -1;
59 }
60
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);
65
66 if ((result = ioctl(netfd, NWIOSTCPCONF, &tcpconf) ) <0)
67 {
68 fprintf(stderr, "Error establishing communication\n");
69 printf("Error: %d\n",result);
70 close(netfd);
71 return -1;
72 }
73
74 /* Get configuration for TCP comm */
75 if ((result = ioctl(netfd, NWIOGTCPCONF, &tcpconf) ) < 0)
76 {
77 fprintf(stderr,"Error getting configuration\n");
78 printf("Error: %d\n", result);
79 close(netfd);
80 return -1;
81 }
82
83 /* Establish connection options (send URG data) */
84 tcpopt.nwto_flags = NWTO_SND_URG_MASK;
85
86 /* Set options for TCP comm */
87 if ((result = ioctl(netfd, NWIOSTCPOPT, &tcpopt) ) < 0)
88 {
89 fprintf(stderr,"Error getting configuration\n");
90 printf("Error: %d\n", result);
91 close(netfd);
92 return -1;
93 }
94
95 /* Get configuration for TCP comm */
96 if ((result = ioctl(netfd, NWIOGTCPOPT, &tcpopt) ) < 0)
97 {
98 fprintf(stderr,"Error getting options\n");
99 printf("Error: %d\n", result);
100 close(netfd);
101 return -1;
102 }
103
104 tcpcl.nwtcl_flags = 0;
105 tries = 0;
106 while (tries < 10) {
107 if ( (result = ioctl(netfd, NWIOTCPCONN, &tcpcl)) < 0 ) {
108 if (errno != EAGAIN)
109 {
110 fprintf(stderr, "Server is not listening\n");
111 close(netfd);
112 return(-1);
113 }
114 fprintf(stderr, "Unable to connect\n");
115 sleep(1);
116 tries++;
117 }
118 else
119 break; /* Connection */
120 }
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;
129 }
130 return netfd;
131}
132
133int 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;
140
141 /* Check parameters */
142 if (argc !=2) {
143 fprintf(stderr,"Usage: %s host\n", argv[0]);
144 exit(-1);
145 }
146
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)
154 {
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);
160 }
161 if (!FD_ISSET(fd, &fds_write)) {
162 fprintf(stderr, "Error: The net connection is not ready for writing (?)\n");
163 exit(-1);
164 }
165
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);
171
172 /* If data sent is exit then break */
173 if (!strcmp(send_buf,"exit"))
174 break;
175
176 /* Get server response */
177 data_read = read(fd, &recv_buf, 1024);
178 printf("Received: %s\n\n", recv_buf);
179 }
180
181 /* Close UDP communication */
182 close(fd);
183}
Note: See TracBrowser for help on using the repository browser.