-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathNonblockTCPEchoClient.c
216 lines (192 loc) · 6.29 KB
/
NonblockTCPEchoClient.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <stdarg.h>
char *tcpdump_timestamp(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
fprintf(stderr, "gettimeofday error!");
exit(1);
}
static char str[30];
/* Tue Feb 12 09:17:48 2019 */
char *ptr = ctime(&tv.tv_sec);
strcpy(str, ptr + 11);
snprintf(str + 8, sizeof(str) - 8, ".%06ld", (long)tv.tv_usec);
return str;
}
ssize_t logx(const char *fmt, ...)
{
size_t n = 0;
va_list ap;
va_start(ap, fmt);
char buf[1024];
n = snprintf(buf, sizeof(buf), "%s: ", tcpdump_timestamp());
vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
va_end(ap);
return fprintf(stderr, "%s\n", buf);
}
void nonblock(int fd)
{
int flags = fcntl(fd, F_GETFL);
flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
}
int max(int a, int b)
{
return a > b ? a : b;
}
int main(int argc, char const *argv[])
{
if (argc != 3) {
printf("usage : %s <ip/hostname> <port/service>\n", argv[0]);
exit(1);
}
const char *hostname = argv[1];
const char *service = argv[2];
struct addrinfo hints, *res = NULL, *ressave = NULL;
bzero(&hints, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
int error = 0;
if ((error = getaddrinfo(hostname, service, &hints, &res)) != 0) {
logx("getaddrinfo error! %s %s : %s", hostname, service, gai_strerror(error));
exit(1);
}
int fd = -1;
do {
if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket error!");
continue;
}
if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
perror("connect error!");
close(fd);
continue;
}
break;
} while ((res = res->ai_next) != NULL);
if (res == NULL) {
logx("client start fail!");
exit(1);
}
ressave = res;
fd_set rset, wset;
const int size = 5 << 10;
char to[size], from[size];
char *to_send, *to_finish, *from_send, *from_finish;
to_send = to_finish = to;
from_send = from_finish = from;
int stdineof = 0;
nonblock(fd);
nonblock(STDOUT_FILENO);
nonblock(STDIN_FILENO);
int maxfd = max(max(fd, STDOUT_FILENO), STDIN_FILENO);
ssize_t nwrite = 0, nread = 0, ntowrite = 0;
for (;;) {
FD_ZERO(&rset);
FD_ZERO(&wset);
/* 标准输入还有内容需要读取,并且待发送缓冲区未填满,标准输入的可读事件 */
if (stdineof == 0 && to_finish < &to[size])
FD_SET(STDIN_FILENO, &rset);
/* 缓冲区中还有待发送的数据,监听套接字的可写事件 */
if (to_send < to_finish)
FD_SET(fd, &wset);
/* 接收缓冲区还有空余,监听套接字的可读事件 */
if (from_finish < &from[size])
FD_SET(fd, &rset);
/* 接收缓冲区还有待读取的数据,监听标准输出的可写事件 */
if (from_send < from_finish)
FD_SET(STDOUT_FILENO, &wset);
select_again:
if (select(maxfd + 1, &rset, &wset, NULL, NULL) < 0) {
if (errno == EINTR)
goto select_again;
perror("select error!");
exit(1);
}
/* 从标准输入读到缓冲区 */
if (FD_ISSET(STDIN_FILENO, &rset)) {
if ((nread = read(STDIN_FILENO, to_send, &to[size] - to_send)) < 0) {
if (errno != EAGAIN) {
perror("read error!");
exit(1);
}
/* 输入了 ctrl+d, 将标准输入关闭标志记上,但先不要 shutdown, 除非从标准输入读到的数据已经全部发完 */
} else if (nread == 0) {
logx("all data from client finished!");
stdineof = 1;
if (to_send == to_finish) {
shutdown(fd, SHUT_WR);
logx("send FIN to server");
}
} else {
logx("read %zd bytes from stdin", nread);
to_send += nread;
FD_SET(fd, &wset);
}
}
if (FD_ISSET(fd, &rset)) {
if ((nread = read(fd, from_send, &from[size] - from_send)) < 0) {
if (errno != EAGAIN) {
perror("read error!");
exit(1);
}
} else if (nread == 0) {
if (stdineof) {
logx("server cut connection");
} else {
logx("server terminated abnormally");
}
break;
} else {
logx("read %zd bytes from server", nread);
from_send += nread;
FD_SET(STDOUT_FILENO, &wset);
}
}
if (FD_ISSET(STDOUT_FILENO, &wset) && (ntowrite = from_send - from_finish) > 0) {
if ((nwrite = write(STDOUT_FILENO, from_finish, ntowrite)) < 0) {
if (errno != EAGAIN) {
perror("write error!");
exit(1);
}
} else {
logx("wrote %zd bytes to stdout", nwrite);
from_finish += nwrite;
if (from_finish == from_send)
from_send = from_finish = from;
}
}
if (FD_ISSET(fd, &wset) && (ntowrite = to_send - to_finish) > 0) {
if ((nwrite = write(fd, to_finish, ntowrite)) < 0) {
if (errno != EAGAIN) {
perror("write error!");
exit(1);
}
} else {
logx("wrote %zd bytes to server", nwrite);
to_finish += nwrite;
if (to_send == to_finish) {
to_send = to_finish = to;
if (stdineof) {
shutdown(fd, SHUT_WR);
logx("halfclose, send FIN to server");
}
}
}
}
}
return 0;
}