博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
epoll 实现Chat
阅读量:4180 次
发布时间:2019-05-26

本文共 3943 字,大约阅读时间需要 13 分钟。

服务端

main函数:

int main(int argc,char ** argv){        int sockfd;    if((sockfd = socket_bulit())==-1){       printf("built socket error\n");       return 0;    }       printf("wait for client connect\n");       do_epoll(sockfd);}

do_epoll函数:

void do_epoll(int sockfd){     int epollfd;     struct epoll_event events[EPOLLEVENTS];     int ret;        epollfd = epoll_create(FILESIZE);     add_event(epollfd,sockfd,EPOLLIN);          while(1){          ret = epoll_wait(epollfd,events,EPOLLEVENTS,-1);          epoll_handle(epollfd,events,ret,sockfd);     }     close(epollfd);}

epoll_handle函数

void epoll_handle(int epollfd,struct epoll_event *events,int num,int sockfd){     int i,fd;     for(i = 0;i

sock_accept函数

void sock_accept(int epollfd,int sockfd){     int clifd;     struct sockaddr_in cliaddr;     socklen_t cliaddrlen = sizeof(struct sockaddr);     clifd = accept(sockfd,(struct sockaddr*)&cliaddr,&cliaddrlen);     if(clifd == -1)        perror("accept error");     else{        printf("accept a client from: %s , %d\n",inet_ntoa(cliaddr.sin_addr),cliaddr.sin_port);        cli[clifd].cliaddr = cliaddr;        cli[clifd].port = cliaddr.sin_port;       // setnonblocking(clifd);        usr[usr_count] = clifd;        usr_count++;        add_event(epollfd,clifd,EPOLLIN);     }}

epoll_write函数:

void epoll_write(int epollfd,int fd){     int nwrite;       nwrite = write(fd,cli[fd].write_buf,strlen(cli[fd].write_buf));     printf("send message to client: %d \n",fd);     if(nwrite == -1){        perror("write error:");        close(fd);        delete_event(epollfd,fd,EPOLLOUT);     }     else        modify_event(epollfd,fd,EPOLLIN);}

epoll_read函数:

void epoll_read(int epollfd,int fd){     int nread;     nread = read(fd,cli[fd].read_buf,maxline);     if(nread == -1){       perror("read error:");       close(fd);       delete_event(epollfd,fd,EPOLLIN);     }     else if(nread == 0){       printf("client close\n");       close(fd);       delete_event(epollfd,fd,EPOLLIN);     }     else{       printf("from client %d read message : %s\n",fd,cli[fd].read_buf);       for(int x=0;x

客户端

main函数:

int main(int argc,char ** argv){      if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){      printf("create socket error\n");      return 0;    }    memset(sendline,0,sizeof(sendline));    memset(recvline,0,sizeof(recvline));    memset(&servaddr,0,sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_port = htons(PORT);    inet_pton(AF_INET,IP,&servaddr.sin_addr);    if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0){      printf("connect error:%s \n",strerror(errno));      return 0;    }         do_epoll(sockfd);    close(sockfd);    return 0;}

epoll_write,epoll_read函数:

void epoll_write(int epollfd,int fd,int sockfd){     int nwrite;     nwrite = write(fd,sendline,strlen(sendline));     if(nwrite == -1){        perror("write error:");        close(fd);     }     else{         if(fd == STDOUT_FILENO){             delete_event(epollfd,fd,EPOLLOUT);         }         else {                      modify_event(epollfd,fd,EPOLLIN);         }     }    memset(sendline,0,sizeof(sendline));}void epoll_read(int epollfd,int fd,int sockfd){     int nread;          if(fd == STDIN_FILENO){         nread = read(fd,sendline,maxline);     }     else{        nread = read(fd,recvline,maxline);        printf("%s",recvline);        memset(recvline,0,sizeof(recvline));      }     if(nread == -1){       perror("read error:");       close(fd);     }     else if(nread == 0){       printf("server close\n");       close(fd);     }     else{                   if(fd == STDIN_FILENO){           add_event(epollfd,sockfd,EPOLLOUT);         }         else {           delete_event(epollfd,sockfd,EPOLLIN);           add_event(epollfd,STDOUT_FILENO,EPOLLOUT);         }     }}

epoll_handle,do_epoll函数

void epoll_handle(int epollfd,struct epoll_event *events,int num,int sockfd){     int i,fd;     for(i = 0;i

效果:

这里写图片描述

你可能感兴趣的文章
带约束的K-means聚类算法
查看>>
约束优化方法
查看>>
VRPTW建模与求解—基于粒子群算法
查看>>
数据结构与算法(1):大O表示法
查看>>
Java学习知识树
查看>>
文科生,你为啥学编程?
查看>>
使用Eclipse时出现Unhandled event loop exception错误的有效解决办法
查看>>
JAVA之路:第一章 JAVA入门初体验
查看>>
菜鸟文科生的java之路:运算符
查看>>
菜鸟文科生的java之路:变量和常量
查看>>
菜鸟文科生的java之路:流程控制语句
查看>>
北海糖:Java初阶练习题
查看>>
不知道什么是数组?看这里就好了
查看>>
文科生北海唐的Java之路:方法(慕课)
查看>>
自学Java的轨迹线路
查看>>
如何更好的隐藏你自己,让我们谈谈什么是封装?
查看>>
文科生北海糖的:Java之路——继承
查看>>
Makefile 中:= ?= += =的区别
查看>>
消灭编译警告(Warning)
查看>>
(GCC) How can I hide "defined but not used" warnings in GCC?
查看>>