-
Notifications
You must be signed in to change notification settings - Fork 37
/
server.c
107 lines (90 loc) · 1.84 KB
/
server.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
/*****************************************************
* The TCP socket server to simulate Partial Packet
* and Merged Packets transfer over TCP connection.
* @author: ideawu
* @link: http://www.ideawu.net/
*****************************************************/
#include "inc.h"
void send_partial_packet(int sock);
void send_merged_packet(int sock);
void send_packets(int sock);
int main(int argc, char **argv){
if(argc < 2){
printf("Usage: %s port\n", argv[0]);
exit(0);
}
int test = 0;
if(argc > 2){
test = 1;
printf("enable debug mode\n");
}
const char *ip = "0.0.0.0";
int port = atoi(argv[1]);
int serv = sock_server(ip, port);
if(serv == -1){
printf("error: %s\n", strerror(errno));
exit(0);
}
printf("server listen on %s:%d\n", ip, port);
while(1){
int sock = sock_accept(serv);
if(sock == -1){
printf("error: %s\n", strerror(errno));
exit(0);
}
if(test){
send_partial_packet(sock);
send_merged_packet(sock);
}else{
send_packets(sock);
}
close(sock);
printf("data sent\n");
}
}
void send_packets(int sock){
char *p;
int n;
p = encode_packet("test");
n = strlen(p);
write(sock, p, n);
free(p);
usleep(10 * 1000);
p = encode_packet("Hello");
n = strlen(p);
write(sock, p, n);
free(p);
usleep(10 * 1000);
p = encode_packet("World!");
n = strlen(p);
write(sock, p, n);
free(p);
usleep(10 * 1000);
}
void send_partial_packet(int sock){
char *p = encode_packet("test");
int n = strlen(p);
for(int i=0; i<n; i++){
write(sock, p+i, 1);
usleep(20 * 1000);
}
free(p);
}
void send_merged_packet(int sock){
char buf[1024];
char *p;
int n;
buf[0] = '\0';
char *ptr = buf;
p = encode_packet("Hello");
n = strlen(p);
strcpy(ptr, p);
free(p);
ptr += n;
p = encode_packet("World!");
n = strlen(p);
strcpy(ptr, p);
free(p);
ptr += n;
write(sock, buf, strlen(buf));
}