Oops, let me resend the source code. It seems copy-as-html caused problems here.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#define MAX_EVENTS 10
char buf[1024];
int main()
{
struct epoll_event ev, events[MAX_EVENTS];
int udp_sock, nfds, epollfd;
struct sockaddr_in server_addr;
if ((udp_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("udp_sock: socket");
exit(EXIT_FAILURE);
}
epollfd = epoll_create1(0);
if (epollfd == -1) {
perror("epoll_create1");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(12345);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(udp_sock, (struct sockaddr*)&server_addr,
sizeof(server_addr)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
ev.events = EPOLLONESHOT | EPOLLIN;
ev.data.fd = udp_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_sock, &ev) == -1) {
perror("epoll_ctl: listen_sock");
exit(EXIT_FAILURE);
}
for (;;) {
int n;
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
if (nfds == -1) {
perror("epoll_wait");
exit(EXIT_FAILURE);
}
for (n = 0; n < nfds; ++n) {
if (events[n].data.fd == udp_sock)
recv(udp_sock, buf, 1024, 0);
}
}
return 0;
}