Repository navigation
Expand file tree
/
Copy pathtest06_cli.c
More file actions
executable file
·162 lines (145 loc) · 3.74 KB
/
Copy pathtest06_cli.c
File metadata and controls
executable file
·162 lines (145 loc) · 3.74 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
/*
* Test name: test06_cli.c
*
* Objective: Test a simple TCP client
*
* Description: Implements a simple echo client using the TCP protocol. First
* it waits until it is possible to write (which is always).
*/
#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 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_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 TCP communication */
close(fd);
}