forked from across-travel/FUCK_TCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
102 lines (85 loc) · 1.9 KB
/
client.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
/*****************************************************
* The TCP socket client that receive simulate Partial Packet
* and Merged Packets from TCP connection.
* @author: ideawu
* @link: http://www.ideawu.net/
*****************************************************/
#include "inc.h"
#include "buffer.h"
char* parse(struct Buffer *buf);
int main(int argc, char **argv){
if(argc <= 2){
printf("Usage: %s ip port\n", argv[0]);
exit(0);
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int sock = sock_connect(ip, port);
if(sock == -1){
printf("error: %s\n", strerror(errno));
exit(0);
}
printf("connected to %s:%d\n", ip, port);
struct Buffer buf;
buffer_init(&buf);
while(1){
char tmp[128];
int len = read(sock, tmp, sizeof(tmp));
if(len <= 0){
printf("receive %d, exit.\n", len);
exit(0);
}
if(buffer_add(&buf, tmp, len) == -1){
printf("error, too large packet!\n");
exit(0);
}
int n = 0;
while(1){
char *msg = parse(&buf);
if(!msg){
break;
}
n ++;
if(n == 1){
printf("< %s\n", msg);
}else{
printf("< %s [Merged Packet]\n", msg);
}
free(msg);
}
}
return 0;
}
char* parse(struct Buffer *buf){
if(buf->size == 0){
return NULL;
}
int head_len;
int body_len;
{
char *body = (char *)memchr(buf->data, '|', buf->size);
if(body == NULL){
printf("[Partial Packet] header not ready, buffer %d\n", buf->size);
return NULL;
}
body ++;
head_len = body - buf->data;
}
{
char header[20];
memcpy(header, buf->data, head_len - 1); // no '|'
header[head_len - 1] = '\0';
body_len = atoi(header);
}
if(buf->size < head_len + body_len){
printf("[Partial Packet] body not ready, buffer %d\n", buf->size);
return NULL;
}
char *body = malloc(body_len + 1);
if(body_len > 0){
memcpy(body, buf->data + head_len, body_len);
}
body[body_len] = '\0';
buffer_del(buf, head_len + body_len);
return body;
}