gt; using namespace std; #define MAXLINE 5 #define OPEN_MAX 100 #define LISTENQ 20 #define SERV_PORT 5000 #define INFTIM 1000 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); } } int main() { int i,maxi,listenfd, connfd,sockfd,epfd,nfds; ssize_t n; char line[MAXLINE]; socklen_t clilen; struct epoll_event ev,events[20]; epfd = epoll_create(256); struct sockaddr_in clientaddr; struct sockaddr_in serveraddr; listenfd = socket(AF_INET,SOCK_STREAM,0); setnonblocking(listenfd); ev.data.fd = listenfd; ev.events=EPOLLIN|EPOLLET; //ev.events=EPOLLIN; epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev); bzero(&serveraddr,sizeof(serveraddr)); serveraddr.sin_family = AF_INET; char* local_addr="127.0.0.1"; inet_aton(local_addr,&(serveraddr.sin_addr)); ev.data.fd = sockfd; ev.events = EPOLLOUT|EPOLLET; //epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev); } else if(events[i].events & EPOLLOUT) { sockfd = events[i].data.fd; write(sockfd,line,n); ev.data.fd = sockfd; ev.events = EPOLLIN|EPOLLET; epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev); } } } return 0; } [windriver@windriver-machine ltest]$ [windriver@windriver-machine ltest]$ cat cli.pl #!/usr/bin/perl use IO::Socket; my $host = "127.0.0.1"; my $port = 5000; my $socket = IO::Socket::INET->new("$host:$port") or die "create socket error $@"; my $msg_out = "1234567890"; print $socket $msg_out; print "now send over,go to sleep...\n"; while(1) { sleep(1); } [windriver@windriver-machine ltest]$
|