Repository navigation
Expand file tree
/
Copy pathtest07_cli.c
More file actions
executable file
·182 lines (163 loc) · 4.31 KB
/
Copy pathtest07_cli.c
File metadata and controls
executable file
·182 lines (163 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Test name: test07_cli.c
*
* Objective: Test an impatient TCP client with timeout which waits input both
* from the terminal and from the network connection.
*
* Description: Implements a echo client using the TCP protocol with a timeout
* value. It waites for data on both the terminal and the network connection and
* prints an angry message asking for more data if there is a timeout.
*
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/asynchio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <net/netlib.h>
#include <net/gen/netdb.h>
#include <net/gen/in.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <net/hton.h>
#define PORT 6060L
int tcp_connect(char *host, long port)
{
/* creates a tcp connection with specified host and port */
char *tcp_device;
struct hostent *hp;
int netfd;
nwio_tcpcl_t tcpcopt;
nwio_tcpconf_t tcpconf;
ipaddr_t dirhost;
int tries;
int result;
/* get host address */
if ((hp = gethostbyname(host)) == (struct hostent*) NULL)
{
fprintf(stderr,"Unknown host\n");
return(-1);
}
memcpy((char *)&dirhost, (char *)hp->h_addr, hp->h_length);
/* Get default TCP device */
if (( tcp_device = getenv("TCP_DEVICE") ) == NULL)
tcp_device = TCP_DEVICE;
/* Establish TCP connection */
if ((netfd = open(tcp_device, O_RDWR)) < 0)
{
fprintf(stderr,"Error opening TCP device\n");
return -1;
}
/* Configure TCP connection */
tcpconf.nwtc_flags=NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
tcpconf.nwtc_remaddr = dirhost;
tcpconf.nwtc_remport = (tcpport_t) htons(port);
if ((result = ioctl(netfd, NWIOSTCPCONF, &tcpconf) ) <0)
{
fprintf(stderr, "Error establishing communication\n");
printf("Error: %d\n",result);
close(netfd);
return -1;
}
/* Get configuration for TCP comm */
if ((result = ioctl(netfd, NWIOGTCPCONF, &tcpconf) ) < 0)
{
fprintf(stderr,"Error getting configuration\n");
printf("Error: %d\n", result);
close(netfd);
return -1;
}
/* Establish connection */
tcpcopt.nwtcl_flags = 0;
tries = 0;
while (tries < 10) {
if ( (result = ioctl(netfd, NWIOTCPCONN, &tcpcopt)) < 0 ) {
if (errno != EAGAIN)
{
fprintf(stderr, "Server is not listening\n");
close(netfd);
return(-1);
}
fprintf(stderr, "Unable to connect\n");
sleep(1);
tries++;
}
else
break; /* Connection */
}
/* Check result value */
if (result < 0) {
fprintf(stderr, "Error connecting\n");
fprintf(stderr, "Error: %d\n", result);
printf("Number of tries: %d\n", tries);
printf("Error: %d\n", errno);
close(netfd);
return -1;
}
return netfd;
}
int main(int argc,char *argv[]) {
int fd;
ssize_t data_read;
char send_buf[1024];
char recv_buf[1024];
fd_set fds_read;
int ret;
struct timeval timeout;
/* Check parameters */
if (argc !=2) {
fprintf(stderr,"Usage: %s host\n", argv[0]);
exit(-1);
}
if ((fd = tcp_connect(argv[1], PORT) ) < 0)
exit(-1);
printf("Connected to server\n");
while (1)
{
/* init fd_set */
FD_ZERO(&fds_read);
FD_SET(0, &fds_read); /* stdin */
FD_SET(fd, &fds_read);
/* set timeout */
timeout.tv_sec = 3;
timeout.tv_usec = 0;
printf("Send data: ");
fflush(stdout);
/* Wait until it is possible to read with select */
ret = select(4, &fds_read, NULL, NULL, &timeout);
if (ret < 0) {
fprintf(stderr, "Error on select waiting for write: %d\n", errno);
exit(-1);
}
/* timeout */
if (ret == 0) {
printf("\nClient says: Hey! I want to send some data!!\n");
continue;
}
/* handle data from network */
if (FD_ISSET(fd, &fds_read)) {
data_read = read(fd, &recv_buf, 1024);
printf("Server says: %s\n\n", recv_buf);
}
/* handle data from terminal */
if (FD_ISSET(0, &fds_read)) {
/* Get a string and send it */
gets(send_buf);
write(fd, &send_buf, strlen(send_buf)+1);
/* If data sent is exit then break */
if (!strcmp(send_buf,"exit"))
break;
/* Get server response */
data_read = read(fd, &recv_buf, 1024);
printf("Received: %s\n\n", recv_buf);
}
}
/* Close TCP communication */
close(fd);
}