c/c++ linux epoll系列1 建立epoll

linux epoll系列1 建立epoll

聽說select和poll的弱點是,隨着鏈接(socket)的增長,性能會直線降低。

epoll不會隨着鏈接(socket)的增長,性能直線降低。

知識點:

1,epoll_wait函數是阻塞的,直到有socket發生變化。

2,epoll使用流程,先建立(epoll_create),再把socket添加到epoll裏(epoll_ctl),而後等待socket的變化(epoll_wait)

接收端,接收2個socketlinux

#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");
    //在這裏會阻塞,直到有socket進來.
    nfds = epoll_wait(epfd, ev_ret, EVENTS, -1);
    if(nfds <= 0){
      perror("epoll_wait");
      return 1;
    }

    printf("after epoll_wait\n");

    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源代碼c++

發送端,向2個地址發送信息git

#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源代碼github

運行方法:shell

先運行接收端,結果以下:微信

before epoll_wait

再運行發送端,結果以下:多線程

before epoll_wait
after epoll_wait
data to port 11111
before epoll_wait
after epoll_wait
data to port 22222
before epoll_wait

從運行結果能夠看出:在epoll_wait處,程序的停住的,也就是阻塞的狀態,可是當運行發送端後,立刻就變成了非阻塞狀態,也就實現了,處理多個socket的請求,而並無使用多進程,或者多線程。

c/c++ 學習互助QQ羣:877684253

本人微信:xiaoshitou5854

相關文章
相關標籤/搜索