添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

linux epoll系列3 利用epoll_wait设置timeout时间长度

epoll_wait函数的第四个参数可以设置,epoll_wait函数的等待时间(timeout时间长度)。

例子1,是接收端。

例子2,是发送端。

例子1,接收端

#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>
#define EVENTS 12
int main(){
  int sock1, sock2;
  sockaddr_in addr1, addr2;
  int epfd;
  epoll_event ev, ev_ret[EVENTS];
  char buf[2048];
  int i;
  int nfds;
  int n;
  //创建2个接受消息的socket
  sock1 = socket(AF_INET, SOCK_DGRAM, 0);
  sock2 = socket(AF_INET, SOCK_DGRAM, 0);
  addr1.sin_family = AF_INET;
  addr2.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &addr1.sin_addr.s_addr);
  inet_pton(AF_INET, "127.0.0.1", &addr2.sin_addr.s_addr);
  addr1.sin_port = htons(11111);
  addr2.sin_port = htons(22222);
  bind(sock1, (sockaddr*)&addr1, sizeof(addr1));
  bind(sock2, (sockaddr*)&addr2, sizeof(addr2));
  //参数不小于0就行
  epfd = epoll_create(1);
  if(epfd < 0){
    perror("epoll_create");
    return 1;
  memset(&ev, 0, sizeof(ev));
  ev.events = EPOLLIN;//只读
  ev.data.fd = sock1;//把sock1加到epoll
  if(epoll_ctl(epfd, EPOLL_CTL_ADD, sock1, &ev) != 0){
    perror("epoll_ctl");
    return 1;
  memset(&ev, 0, sizeof(ev));
  ev.events = EPOLLIN;//只读
  ev.data.fd = sock2;//把sock2加到epoll
  if(epoll_ctl(epfd, EPOLL_CTL_ADD, sock2, &ev) != 0){
    perror("epoll_ctl");
    return 1;
  while(1){
    printf("before epoll_wait\n");
    //在这里会阻塞,只等待10秒,超过10秒,epoll_wait函数结束,返回0给nfds.
    nfds = epoll_wait(epfd, ev_ret, EVENTS, 10*1000);
    if(nfds < 0){
      perror("epoll_wait");
      return 1;
    printf("after epoll_wait\n");
    if(nfds == 0){
      printf("timeout\n");
      break;
    for(i = 0; i < nfds; ++i){
      //判断进来的socket是哪个socket
      if(ev_ret[i].data.fd == sock1){
	//从sock1读取数据,并写入到标准输出
	n = recv(sock1, buf, sizeof(buf), 0);
	write(fileno(stdout), buf, n);
      //判断进来的socket是哪个socket
      else if(ev_ret[i].data.fd == sock2){
	//从sock1读取数据,并写入到标准输出
	n = recv(sock2, buf, sizeof(buf), 0);
	write(fileno(stdout), buf, n);
  close(sock1);
  close(sock2);
  return 0;

github源代码

例子2, 是发送端。

#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 sock;
  sockaddr_in dest1, dest2;
  char buf[1024];
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  dest1.sin_family = AF_INET;
  dest2.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &dest1.sin_addr.s_addr);
  inet_pton(AF_INET, "127.0.0.1", &dest2.sin_addr.s_addr);
  dest1.sin_port = htons(11111);
  dest2.sin_port = htons(22222);
  strcpy(buf, "data to port 11111\n");
  //给地址1(dest1)送信
  sendto(sock, buf, strlen(buf), 0, (sockaddr*)&dest1, sizeof(dest1));
  strcpy(buf, "data to port 22222\n");
  //给地址2(dest2)送信
  sendto(sock, buf, strlen(buf), 0, (sockaddr*)&dest2, sizeof(dest1));
  close(sock);
  return 0;

github源代码

运行方法:先运行接收端,再运行发送端。

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854