1. 程式人生 > >Linux網路程式設計epoll模型

Linux網路程式設計epoll模型

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <errno.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>

#include <iostream>
using namespace std;

#define LISTENQ 5 
#define OPEN_MAX 1024
#define SERV_PORT  10088
#define MAX_LINE 1024
#define INFTIM -1 

#define MAXEVENTS 1000

void echo_srv(int clientFd)
{
	//處理使用者請求資料
	char line[MAX_LINE];
	cout << "開始讀取資料" << endl;
	int n = read(clientFd, line, sizeof(line));
	if(n < 0)
	{
		//#define ECONNRESET 104  /* Connection reset by peer */
		if(errno == ECONNRESET)
		{
			close(clientFd);
			printf("異常退出\n");
		}
		else
		{
				printf("網路異常");
				exit(-1);
		}
	}
	else if(n == 0)
	{
		close(clientFd);
		printf("正常退出\n");
	}
	else
	{
		line[n] = 0;
		printf("接收到資料:%s\n", line);
		write(clientFd, line, n);
	}
}

int main(int argc, char** argv)
{
	struct sockaddr_in cliaddr, servaddr;
	int listenFd = socket(AF_INET, SOCK_STREAM, 0);
	if( listenFd < 0)
	{
		printf("socket函式執行失敗");
		return 1;
	}
	
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	//inet_aton('10.132.10.64', &(servaddr.sin_addr));
	//servaddr.sin_addr.s_addr = inet_addr("10.132.10.64");
	servaddr.sin_port = htons(SERV_PORT);
	if(bind(listenFd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
	{
	    printf("bind函式執行失敗");
	  	return 1;
	}
	
	if(listen(listenFd, LISTENQ) < 0)
	{
			printf("listen函式執行失敗");
	  	return 1;
	}
	printf("listen函式執行成功\n");
	//epoll部分
	int epollFd;
	if ((epollFd = epoll_create(1)) == -1) {
        cerr << "create epoll_create falid" << endl;
        return false;
   }
   
    epoll_event epollEvent;
    epollEvent.data.fd = listenFd;
    epollEvent.events = EPOLLIN | EPOLLET;
    if (epoll_ctl(epollFd, EPOLL_CTL_ADD, listenFd, &epollEvent) != 0) 
    {
        cerr << "create epoll err!" << endl;
        return 1;
    }
		cout << "create epoll ok!" << endl;
		epoll_event *pEpollEvents = new epoll_event[MAXEVENTS];
		
		while(true)
		{
			int n = epoll_wait(epollFd, pEpollEvents, MAXEVENTS, -1);
			
			cout << "epoll wait conn" << endl;
			for(int i = 0; i < n; i++)
			{
				epoll_event& e = pEpollEvents[i];
				if((e.events & EPOLLERR) || (e.events & EPOLLHUP) || (!(e.events & EPOLLIN)))
				{
					 cerr << "epoll error for client " << e.data.fd << endl;
					 close(e.data.fd);
				}
				else if(listenFd == e.data.fd)  //處理使用者連線
				{
						socklen_t clilen = sizeof(cliaddr);
						int connfd = accept(listenFd, (struct sockaddr*)&cliaddr, &clilen);
						if(connfd < 0)
			 			{
			 				if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) 
			 				{
            		cerr << "fail to accept new client " << endl;
            		continue;
       			  }
						}
						printf("Ip: %s 到此一遊\n", inet_ntoa(cliaddr.sin_addr));
						
						//將使用者連線加入epoll中進行監視
						e.data.fd = connfd;
    				e.events = EPOLLIN | EPOLLET;
   				  if (epoll_ctl(epollFd, EPOLL_CTL_ADD, connfd, &e) != 0) 
            {
                 cerr << "create epoll err!" << endl;
                 continue;
            }
				}
				else  
				{
					echo_srv(e.data.fd);
				}
				
			}
		}
}