Repository navigation
Expand file tree
/
Copy pathtest08_cli.c
More file actions
executable file
·183 lines (163 loc) · 4.21 KB
/
Copy pathtest08_cli.c
File metadata and controls
executable file
·183 lines (163 loc) · 4.21 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
183
/*
* Test name: test08_cli.c
*
* Objective: Test select on urgent data. TCP client.
*
* Description: It is based on test06_cli but sends urgent data.
*
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/select.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 tcpcl;
nwio_tcpopt_t tcpopt;
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 options (send URG data) */
tcpopt.nwto_flags = NWTO_SND_URG_MASK;
/* Set options for TCP comm */
if ((result = ioctl(netfd, NWIOSTCPOPT, &tcpopt) ) < 0)
{
fprintf(stderr,"Error getting configuration\n");
printf("Error: %d\n", result);
close(netfd);
return -1;
}
/* Get configuration for TCP comm */
if ((result = ioctl(netfd, NWIOGTCPOPT, &tcpopt) ) < 0)
{
fprintf(stderr,"Error getting options\n");
printf("Error: %d\n", result);
close(netfd);
return -1;
}
tcpcl.nwtcl_flags = 0;
tries = 0;
while (tries < 10) {
if ( (result = ioctl(netfd, NWIOTCPCONN, &tcpcl)) < 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_write;
int ret;
/* 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");
/* init fd_set */
FD_ZERO(&fds_write);
FD_SET(fd, &fds_write);
while (1)
{
/* Wait until it is possible to write with select */
ret = select(4, NULL, &fds_write, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Error on select waiting for write: %d\n", errno);
exit(-1);
}
if (!FD_ISSET(fd, &fds_write)) {
fprintf(stderr, "Error: The net connection is not ready for writing (?)\n");
exit(-1);
}
/* Get a string and send it */
printf("Ready to write...\n");
printf("Send data: ");
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 UDP communication */
close(fd);
}