一个简单的基于epoll的udp接收
阅读原文时间:2023年07月08日阅读:5

---

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14
15 #include
16
17
18 using namespace std;
19
20 int main(int argc, char *argv[])
21 {
22
23 int udpfd = 0;
24 char arr_ip4_local[] = {"192.168.244.128"};
25 char arr_ip4_dst[] = {"10.1.1.5"};
26
27
28 struct sockaddr_in addr_dst;
29 struct sockaddr_in addr_local;
30 bzero(&addr_dst, sizeof(struct sockaddr_in));
31 bzero(&addr_local, sizeof(struct sockaddr_in));
32
33 addr_dst.sin_port = htons(12345);
34 addr_dst.sin_family = AF_INET;
35 // addr_dst.sin_addr.s_addr = htonl(INADDR_ANY);
36 inet_pton(AF_INET, arr_ip4_dst, &addr_dst.sin_addr.s_addr);
37
38 addr_local.sin_family = AF_INET;
39 addr_local.sin_port = htons(12345);
40 //inet_pton(AF_INET, arr_ip4_local, &addr_local.sin_addr.s_addr);
41 addr_local.sin_addr.s_addr = htonl(INADDR_ANY);
42
43 // socket
44 udpfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
45 if (0 > udpfd)
46 {
47 std::cout << "created failed\n"; 48 return 0; 49 } 50 51 52 // bind local address to recv and send data 53 int ret_val = bind(udpfd, (struct sockaddr*)&addr_local, sizeof(addr_local)); 54 if (0 != ret_val) 55 { 56 std::cout << "bind error, id = " << errno << "\n"; 57 close(udpfd); 58 return 0; 59 } 60 61 62 // epoll operation 63 struct epoll_event event; 64 struct epoll_event event_wait; 65 66 int epoll_fd = epoll_create(10); 67 if ( -1 == epoll_fd ) 68 { 69 std::cout << "epoll_create error, id = " << errno << "\n"; 70 close(udpfd); 71 return 0; 72 } 73 74 // 75 event.data.fd = 0; 76 event.events = EPOLLIN; 77 78 // register functions 79 ret_val = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &event); 80 if ( -1 == ret_val ) 81 { 82 std::cout << "epoll_ctl error, id = " << errno << "\n"; 83 close(udpfd); 84 85 return 0; 86 } 87 88 // 89 event.data.fd = udpfd; 90 event.events = EPOLLIN; 91 ret_val = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, udpfd, &event); 92 if ( -1 == ret_val ) 93 { 94 std::cout << "epoll_ctl error, udpfd, id = " << errno << "\n"; 95 close(udpfd); 96 97 return 0; 98 } 99 100 // listen 101 struct sockaddr_in addr_recv; 102 char arr_recv[1024] = {0}; 103 int addr_recv_len = sizeof(addr_recv); 104 bzero(&addr_recv, addr_recv_len); 105 socklen_t recv_len = sizeof(addr_recv); 106 107 int quit_index = 1; 108 int quit_count = 10; 109 110 111 while (1) 112 { 113 std::cout << "wait for connecting: "; 114 ret_val = epoll_wait(epoll_fd, &event_wait, 2, -1); 115 if (-1 == ret_val) 116 { 117 std::cout << "error, while, id = " << errno << "\n"; 118 close(udpfd); 119 return 0; 120 } 121 122 else if (0 < ret_val) 123 { 124 if ( (udpfd == event_wait.data.fd) 125 && 126 (EPOLLIN == (event_wait.events & EPOLLIN)) ) 127 { 128 ret_val = recvfrom(udpfd, arr_recv, 1024, 0, (struct sockaddr*)&addr_recv, &recv_len); 129 std::cout << "\n recvfrom = " << ret_val << "\n"; 130 bzero(&addr_recv, addr_recv_len); 131 quit_index ++; 132 if ( quit_index >= quit_count )
133 break;
134 }
135
136 }
137 else if (0 == ret_val)
138 {
139 std::cout << "\nwhile timeout\n";
140 }
141 else
142 {
143
144 }
145 }
146
147 close(udpfd);
148
149 return 0;
150 }

----

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器