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.
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #include <sys/select.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>
29 int tcp_connect(char *host
, long port
)
31 /* creates a tcp connection with specified host and port */
37 nwio_tcpconf_t tcpconf
;
42 /* get host address */
43 if ((hp
= gethostbyname(host
)) == (struct hostent
*) NULL
)
45 fprintf(stderr
,"Unknown host\n");
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");
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
);
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
);
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
);
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
);
104 tcpcl
.nwtcl_flags
= 0;
107 if ( (result
= ioctl(netfd
, NWIOTCPCONN
, &tcpcl
)) < 0 ) {
110 fprintf(stderr
, "Server is not listening\n");
114 fprintf(stderr
, "Unable to connect\n");
119 break; /* Connection */
121 /* Check result value */
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
);
133 int main(int argc
,char *argv
[]) {
141 /* Check parameters */
143 fprintf(stderr
,"Usage: %s host\n", argv
[0]);
147 if ((fd
= tcp_connect(argv
[1], PORT
) ) < 0)
149 printf("Connected to server\n");
152 FD_SET(fd
, &fds_write
);
155 /* Wait until it is possible to write with select */
156 ret
= select(4, NULL
, &fds_write
, NULL
, NULL
);
158 fprintf(stderr
, "Error on select waiting for write: %d\n", errno
);
161 if (!FD_ISSET(fd
, &fds_write
)) {
162 fprintf(stderr
, "Error: The net connection is not ready for writing (?)\n");
166 /* Get a string and send it */
167 printf("Ready to write...\n");
168 printf("Send data: ");
170 write(fd
, &send_buf
, strlen(send_buf
)+1);
172 /* If data sent is exit then break */
173 if (!strcmp(send_buf
,"exit"))
176 /* Get server response */
177 data_read
= read(fd
, &recv_buf
, 1024);
178 printf("Received: %s\n\n", recv_buf
);
181 /* Close UDP communication */