epoll簡化版本

2021-09-01 04:29:16 字數 3439 閱讀 6475

epoll基於socket

先上socket服務端

#include 

#include

#include

#include

#include

#include

#include

#define maxpending 5 /* max connection requests */

#define buffsize 32

void die(char *mess)

void handleclient(int sock)

while (received > 0)

if ((received = recv(sock, buffer, buffsize, 0)) < 0)

}close(sock);

}int main(int argc, char *argv)

if ((serversock = socket(pf_inet, sock_stream, ipproto_tcp)) < 0)

memset(&echoserver, 0, sizeof(echoserver)); /* clear struct */

echoserver.sin_family = af_inet; /* internet/ip */

echoserver.sin_addr.s_addr = htonl(inaddr_any); /* incoming addr */

echoserver.sin_port = htons(atoi(argv[1])); /* server port */

if (bind(serversock, (struct sockaddr *) &echoserver,sizeof(echoserver)) < 0)

if (listen(serversock, maxpending) < 0)

while (1)

fprintf(stdout, "client connected: %s\n",inet_ntoa(echoclient.sin_addr));

handleclient(clientsock);}}

client

#include

#include

#include

#include

#include

#include

#include

#define buffsize 32

void die(char *mess)

int main(int argc, char *argv)

if ((sock = socket(pf_inet, sock_stream, ipproto_tcp)) < 0)

memset(&echoserver, 0, sizeof(echoserver)); /* clear struct */

echoserver.sin_family = af_inet; /* internet/ip */

echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* ip address */

echoserver.sin_port = htons(atoi(argv[3])); /* server port */

if (connect(sock,(struct sockaddr *) &echoserver,sizeof(echoserver)) < 0)

echolen = strlen(argv[2]);

if (send(sock, argv[2], echolen, 0) != echolen)

fprintf(stdout, "received: ");/* receive the word back from the server */

while (received < echolen)

received += bytes;

buffer[bytes] = '\0'; /* assure null terminated string */

fprintf(stdout, buffer);

}fprintf(stdout, "\n");

close(sock);

exit(0);

}

epoll在這個基礎上改造一下

epolltest.c

#include 

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

#define maxline 5

#define open_max 100

#define listenq 20

#define serv_port 5000

#define inftim 1000

void setnonblocking(int sock)

opts = opts|o_nonblock;

if(fcntl(sock,f_setfl,opts)<0)

}int main()

//setnonblocking(connfd);

char *str = inet_ntoa(clientaddr.sin_addr);

cout << "accapt a connection from " << str << endl;

ev.data.fd=connfd;//設定用於讀操作的檔案描述符

ev.events=epollin|epollet; 設定用於注測的讀操作事件

epoll_ctl(epfd,epoll_ctl_add,connfd,&ev);//註冊ev

}else if(events[i].events&epollin) else

} else if (n == 0)

line[n] = '\0';

cout << "read " << line << endl;

ev.data.fd=sockfd;//描述符

ev.events=epollout|epollet;//設定用於注測的寫操作事件 //修改sockfd上要處理的事件為epollout //epoll_ctl(epfd,epoll_ctl_mod,sockfd,&ev);

}else if(events[i].events&epollout)}}

return 0;

}

報表製作簡化版

機房收費系統無論是重構還是第一版,都用到了報表,為什麼在乙個系統中要新增報表呢?報表的作用是什麼呢?報表百科。我理解的報表是 向上級報告情況的乙個媒介,沒有固定的格式。之前在專案中,我們真正給企業做過一次報表,是以匯出word的形式生成的,大概格式如圖 這次在自己的 機房收費系統 中設計報表,我使用...

飛機大戰 簡化版

import pygame from pygame.locals import import random import time class herobullet def init self,x,y,windows self.x x self.y y self.windows windows se...

簡化版桶排序

例 讓計算機隨機讀入 5個數然後將這 5個數從大到小輸出 輸入 5 3 5 2 8 輸出 8 5 5 3 2 思路 先申請乙個大小為 11 的陣列 int a 11 現在你已經有了 11 個變數,編號從 a 0 a 10 剛開始,我們將 a 0 a 10 都初始化為 0,表示這些0 10的數字還沒出...