本文共 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