-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTCPRecvFlagTestServer_MSG_WAITALL.c
74 lines (64 loc) · 1.79 KB
/
TCPRecvFlagTestServer_MSG_WAITALL.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include "../../tool/AddrTool.h"
int main(int argc, char const *argv[])
{
if (argc != 2) {
printf("usage : %s <port>\n", argv[0]);
exit(1);
}
int fd = -1;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket fd create fail!");
exit(1);
}
struct sockaddr_in svraddr, cliaddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(atoi(argv[1]));
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
int on = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
perror("setsockopt erro!");
exit(1);
}
if (bind(fd, (struct sockaddr *)&svraddr, sizeof(svraddr)) < 0) {
perror("bind error!");
exit(1);
}
if (listen(fd, 1000) < 0) {
perror("listen error!");
exit(1);
}
socklen_t clilen = sizeof(cliaddr);
int connfd = -1;
if ((connfd = accept(fd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
perror("accept error!");
exit(1);
}
char *srcInfo = getAddrInfo(&cliaddr);
char *dstInfo = getSockInfo(connfd);
printf("TCP connection from %s to %s\n", srcInfo, dstInfo);
free(srcInfo);
free(dstInfo);
char buf[1024];
bzero(buf, sizeof(buf));
ssize_t nread = 0;
for (;;) {
if ((nread = recv(connfd, buf, 26, MSG_WAITALL)) < 0) {
perror("recv error!");
exit(1);
} else if (nread == 0) {
printf("client send FIN cut connection\n");
close(connfd);
break;
}
printf("recv return and nread = %zd\n", nread);
}
return 0;
}