forked from yuebaixiao/network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepoll-7-recv.cpp
45 lines (35 loc) · 826 Bytes
/
epoll-7-recv.cpp
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
int main(){
int sock0;
sockaddr_in addr;
sockaddr_in client;
socklen_t len;
int sock;
int n;
char buf[65536];
int i;
sock0 = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
addr.sin_addr.s_addr = INADDR_ANY;
bind(sock0, (sockaddr*)&addr, sizeof(addr));
listen(sock0, 5);
len = sizeof(client);
sock = accept(sock0, (sockaddr*)&client, &len);
printf("after accept\n");
for(i = 0; i < 10; ++i){
sleep(2);
n = read(sock, buf, sizeof(buf));
printf("recv data size:[%d] bytes\n", n);
}
printf("close socket and finish\n");
close(sock);
return 0;
}