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>
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>
33 int tcp_connect(char *host
, long port
)
35 /* creates a tcp connection with specified host and port */
40 nwio_tcpconf_t tcpconf
;
45 /* get host address */
46 if ((hp
= gethostbyname(host
)) == (struct hostent
*) NULL
)
48 fprintf(stderr
,"Unknown host\n");
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");
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
);
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
);
86 /* Establish connection */
87 tcpcopt
.nwtcl_flags
= 0;
90 if ( (result
= ioctl(netfd
, NWIOTCPCONN
, &tcpcopt
)) < 0 ) {
93 fprintf(stderr
, "Server is not listening\n");
97 fprintf(stderr
, "Unable to connect\n");
102 break; /* Connection */
104 /* Check result value */
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
);
116 int main(int argc
,char *argv
[]) {
123 struct timeval timeout
;
125 /* Check parameters */
127 fprintf(stderr
,"Usage: %s host\n", argv
[0]);
131 if ((fd
= tcp_connect(argv
[1], PORT
) ) < 0)
133 printf("Connected to server\n");
140 FD_SET(0, &fds_read
); /* stdin */
141 FD_SET(fd
, &fds_read
);
146 printf("Send data: ");
148 /* Wait until it is possible to read with select */
149 ret
= select(4, &fds_read
, NULL
, NULL
, &timeout
);
151 fprintf(stderr
, "Error on select waiting for write: %d\n", errno
);
156 printf("\nClient says: Hey! I want to send some data!!\n");
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 */
168 write(fd
, &send_buf
, strlen(send_buf
)+1);
170 /* If data sent is exit then break */
171 if (!strcmp(send_buf
,"exit"))
174 /* Get server response */
175 data_read
= read(fd
, &recv_buf
, 1024);
176 printf("Received: %s\n\n", recv_buf
);
180 /* Close TCP communication */