![](/img/trans.png)
[英]UDP Multiple sockets Receive data and process efficiently - C & Linux
[英]Multiple UDP sockets using epoll - Not able to receive data
我試圖使用epoll從多個UDP套接字接收數據。 這是一個測試程序,它沒有確切數量的套接字。(為了測試目的,它設置為50.)下面的程序沒有收到任何數據,它被卡在epoll_wait(),因為我已經給它-1等待直到得到數據。 但是沒有得到任何。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <fcntl.h>
#define MAX_CON (50)
typedef struct UDPMessage
{
unsigned short hdr;
unsigned short CC1;
unsigned short CC2;
unsigned char data[1472];
unsigned short CC3;
} UDPPacket;
static struct epoll_event *events;
int create_sockets(unsigned int port);
int create_sockets(unsigned int port)
{
int sock, flag = 1;
struct sockaddr_in sock_name;
/* Create a datagram socket*/
sock = socket(PF_INET, SOCK_DGRAM, 0);
/* Setting the socket to non blocking*/
fcntl(sock, F_SETFL, O_NONBLOCK);
if (sock < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
/* Set the reuse flag. */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0)
{
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
exit(EXIT_FAILURE);
}
/* Give the socket a name. */
sock_name.sin_family = AF_INET;
sock_name.sin_port = htons(port);
sock_name.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock,(struct sockaddr *) &sock_name, sizeof(sock_name)) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
return sock;
}
int main(int argc, char *argv[])
{
fd_set master;
fd_set read_fds;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
int fdmax=0;
int newfd;
int nbytes;
int epfd = -1;
int res = -1;
struct epoll_event ev;
int i=0;
int index = 0;
int client_fd = -1;
int SnumOfConnection = 0;
UDPPacket UDPPacket_obj;
struct sockaddr_in client_sockaddr_in;
struct sockaddr_in server_sockaddr_in;
int sockfd[12]={0};
int PORTS[12] = { 10011,10012,10013,10021,10022,10023,10031,10032,10033,10041,10042,10043}; //TEST only 12 ports
int port_index=0;
int sockaddr_in_length=0;
printf("In main\n");
/* Create sockets with the above list of ports */
for( port_index=0;port_index<12;port_index++)
{
sockfd[port_index]=create_sockets(PORTS[port_index]);
if(sockfd[port_index] > fdmax)
fdmax = sockfd[port_index];
}
printf("after soc creation\n");
sockaddr_in_length = sizeof(struct sockaddr_in);
events = calloc(MAX_CON, sizeof(struct epoll_event));
if ((epfd = epoll_create(MAX_CON)) == -1) /* epoll_create */
{
perror("epoll_create");
exit(1);
}
ev.events = EPOLLIN;
ev.data.fd = fdmax;
for( port_index=0;port_index<12;port_index++)
{
if (epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd[port_index], &ev) < 0) /* epoll_ctl */
{
perror("epoll_ctl");
exit(1);
}
}
while(1)
{
printf("Before epoll_wait\n");
res = epoll_wait(epfd, events, MAX_CON, -1); /* epoll_wait */
printf("After epoll_wait\n");
client_fd = events[index].data.fd;
for (index = 0; index < MAX_CON; index++)
{
printf("start\n");
if (events[index].events & EPOLLHUP)
{
if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &ev) < 0) {
perror("epoll_ctl");
}
close(client_fd);
break;
}
printf("before EPOLLIN check\n");
if (events[index].events & EPOLLIN) {
if((nbytes = recvfrom(client_fd,&UDPPacket_obj,sizeof(UDPPacket),0,
(struct sockaddr *)&client_sockaddr_in,
&sockaddr_in_length)) <=0)
{
if(nbytes == 0) {
printf("socket %d hung up\n", client_fd);
}
else {
printf("recv() error lol! %d", client_fd);
perror("");
}
if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &ev) < 0) {
perror("epoll_ctl");
}
//close(client_fd);
}
printf("Data recv=%s\n",UDPPacket_obj.data);
}
}
} //end of while
return 0;
}
您只等待單個套接字上的事件,即fdmax
中的fdmax
。
您需要添加要輪詢的所有套接字。
這是一個非常古老的線程。
但進入while(1)循環,行:
client_fd = events[index].data.fd;
完全相同:
client_fd = fdmax ;
更清楚:
(A)你想要推薦一個fd。 這是您在命令中傳遞的那個
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd[port_index], &ev)
被輪詢的fd是sockfd [port_index]
(B)並且在返回的事件結構中有你要測試的fd。 在您的代碼中,您可以在以下行中訪問它:
client_fd = events[index].data.fd;
這個fd可能不是相同的輪詢。 返回的fd是在您通過以下方式傳遞給epoll_ctl的事件結構中:
ev.data.fd = fdmax;
:
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd[port_index], &ev)
返回的fd是de ev內部。 即使您在另一個套接字中收到一個事件,您獲得的事件結構在event.data.fd中也有fd_max。
所以,當你執行:
client_fd = events[index].data.fd;
你總是得到fd_max。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.