随笔 - 23  文章 - 0 评论 - 68 
<2007年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

广告中国绩效网,注册立刻送10元 广告中国绩效网,注册立刻送10元

常用链接

留言簿(7)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

看了很多的资料,决定自己写个程序练习练习,于是就自己写了个小程序来练练手:
Server端(linux环境下):
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
 #include <arpa/inet.h>
 #include <fcntl.h>


#define PORT    3456
#define BACKLOG   10
#define MAX_EPOLL_FD 65535

int CreateTcpListenSocket();
int InitEpollFd();
void UseConnectFd(int sockfd);
void setnonblocking(int sock);

static int listenfd;

int main()
{
 int epoll_fd;
 int nfds;
 int i;
 
 struct epoll_event events[50];
 struct epoll_event tempEvent;

 int sockConnect;
 struct sockaddr_in remoteAddr;
 int addrLen;

 addrLen = sizeof(struct sockaddr_in);
 epoll_fd = InitEpollFd();
 if (epoll_fd == -1)
 {
  perror("init epoll fd error.");
  exit(1);
 }

 printf("begin in loop.\n");
 while (1)
  {
  nfds = epoll_wait(epoll_fd, events, 50, 1000);
  //sleep(3);
  printf("connect num: %d\n", nfds);
  if (nfds == -1)
   {
   perror("epoll_wait error.");
   }
  for (i = 0; i < nfds; i++)
   {
   if (listenfd == events[i].data.fd)
    {
    printf("connected success\n");
    sockConnect = accept(events[i].data.fd, (struct sockaddr*)&remoteAddr, &addrLen);
    if (sockConnect == -1)
     {
     perror("accept error.");
     continue;
     }
    setnonblocking(sockConnect);
    tempEvent.events = EPOLLIN | EPOLLET;
    tempEvent.data.fd = sockConnect;
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockConnect, &tempEvent) < 0)
     {
     perror("epoll ctl error.");
     return -1;
     }
    }
   else
    {
    UseConnectFd(events[i].data.fd);
    }
   }
  }
 
}

int CreateTcpListenSocket()
{
 int sockfd;
 struct sockaddr_in localAddr;
 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
 {
  perror("create socket fail");
  return -1;
 }

 setnonblocking(sockfd);
 
 bzero(&localAddr, sizeof(localAddr));
 localAddr.sin_family = AF_INET;
 localAddr.sin_port = htons(PORT);
 localAddr.sin_addr.s_addr = htonl(INADDR_ANY);

 if (bind(sockfd,  (struct sockaddr*)&localAddr, sizeof(struct sockaddr)) == -1)
 {
  perror("bind error");
  return -1;
 }

 if (listen(sockfd, BACKLOG) == -1)
 {
  perror("listen error");
  return -1;
 }

 return sockfd;
}

int InitEpollFd()
{
 //epoll descriptor
 int s_epfd;
 struct epoll_event ev;
 

 listenfd = CreateTcpListenSocket();

 if (listenfd == -1)
 {
  perror("create tcp listen socket error");
  return -1;
 }

 s_epfd = epoll_create(MAX_EPOLL_FD);
 ev.events = EPOLLIN;
 ev.data.fd = listenfd;
 if (epoll_ctl(s_epfd, EPOLL_CTL_ADD, listenfd, &ev) < 0)
 {
  perror("epoll ctl error");
  return -1;
 }

 return s_epfd;
}

void UseConnectFd(int sockfd)
{
 char recvBuff[1500];
 int recvNum = 0;

 recvNum = recv(sockfd, recvBuff, 1500, 0);
 if (recvNum == -1)
  {
  perror("recv error.");
  return;
  }

 recvBuff[recvNum] = '\0';
 printf("message: %s \n", recvBuff);
}

void setnonblocking(int sock)

{

     int opts;

     opts=fcntl(sock,F_GETFL);

     if(opts<0)

     {

          perror("fcntl(sock,GETFL)");

          exit(1);

     }

     opts = opts|O_NONBLOCK;

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

     {

          perror("fcntl(sock,SETFL,opts)");

          exit(1);

     }   

}

client端(VC6.0):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
//#include <sockets.h>
//#include <unistd.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <arpa/inet.h>
#pragma comment(lib,"wsock32.lib")
#define PORT    3456

int main()
{
 int sockfd;
 struct sockaddr_in remoteAddr;
 char mesg[] = "This is a client for epoll test";
 WSADATA wsdata;

 memset(&remoteAddr, 0, sizeof(remoteAddr));
 remoteAddr.sin_family = AF_INET;
 remoteAddr.sin_port = htons(PORT);
 remoteAddr.sin_addr.s_addr = inet_addr("10.40.8.15");

 
 WSAStartup(0x0202,&wsdata);

 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  {
  perror("create socket fail.");
  return -1;
  }
 WSAAsyncSelect(sockfd, NULL, NULL, 0);
 printf("server ip is: %s\n", (char *)inet_ntoa(remoteAddr.sin_addr));

// while (1)
// {
  if (connect(sockfd, (struct sockaddr*)&remoteAddr, sizeof(remoteAddr)) == -1)
  {
   //perror("connect error.");
   printf("Error code: %d\n", WSAGetLastError());
   return -1;
  }
  //printf("times\n");
// }

 printf("connected!\n");
 while (1)
  {
  
  send(sockfd, mesg, strlen(mesg), 0);
  printf("send over\n");
  Sleep(1000);
  }
}

posted on 2008-02-19 18:17 吴剑 阅读(990) 评论(2)  编辑 收藏 引用 所属分类: 我的原创

FeedBack:
# re: epoll的用法 2008-02-28 14:17 we
没有注释的源码 等待消亡的故事  回复  更多评论
  
# re: epoll的用法 2012-10-18 21:55 wr
写的不错。  回复  更多评论
  
只有注册用户登录后才能发表评论。