亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 4515 | 回復(fù): 8
打印 上一主題 下一主題

[C] epoll 簡(jiǎn)單例子 求助 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-08-23 18:47 |只看該作者 |倒序?yàn)g覽
我現(xiàn)在再用epoll 進(jìn)行測(cè)試 但是總有個(gè)問題 比較郁悶 如下:

server 端   
監(jiān)聽的socket 為阻塞模式
epoll 設(shè)為 ET 模式

在監(jiān)聽的過(guò)程中以下面的模式主
while(1){
     epoll_wait();
     if(events.data.fd==listenfd)
     {
          //處理 新來(lái)的socket 連接  
         // 調(diào)用 accept  產(chǎn)生新的fd
        // 把新的fd 設(shè)為非阻塞
       // 以ET模式加入到epoll中
      } else if(events.events&EPOLLIN){
            //處理讀事件     
      }else if(events.events&EPOLLOUT) {
           //處理寫事件
      }
}

client  端

建立一個(gè)socket 阻塞模式  
調(diào)用 connect

不調(diào)用任何函數(shù) 直接sleep(1000000)


上述 過(guò)程執(zhí)行后 server端 出現(xiàn)一次 連接信號(hào)(events.data.fd==listenfd)
出現(xiàn)一次 寫請(qǐng)求(events.events&EPOLLOUT)


我的問題是  在客戶端 我沒有調(diào)用 recv  為什么在server 會(huì)出現(xiàn) 寫請(qǐng)求?


各位大俠 給點(diǎn)指點(diǎn) 。。。。。 有關(guān)這方面的資料討論也行啊 。。。
跪謝。。



下面是詳細(xì)代碼 server

#include <iostream>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>

using namespace std;

#define MAXLINE 100
#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);
    }
}
void sig_quit(int no)
{
   cout<<"donothing"<<endl;
   
   signal(SIGPIPE,sig_quit);
}

main()
{
   int port=5000;
   int epfd;
   int listenfd;
   int nfds;
   int sockfd;
   struct epoll_event ev;
   struct epoll_event events[20];
   epfd=epoll_create(256);

   struct sockaddr_in caddr;
   struct sockaddr_in saddr;

   listenfd=socket(AF_INET,SOCK_STREAM,0);
  
  // setnonblocking(listenfd);

   ev.data.fd=listenfd;
   ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
  
   epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);

   bzero(&saddr,sizeof(struct sockaddr_in));

   saddr.sin_family=AF_INET;
   saddr.sin_port=htons(port);
   saddr.sin_addr.s_addr=INADDR_ANY;
  
   bind(listenfd,(sockaddr*)&saddr,sizeof(struct sockaddr_in));

   listen(listenfd,LISTENQ);

    signal(SIGPIPE,sig_quit);
   
   while(1){
      
       nfds=epoll_wait(epfd,events,20,-1);      

       int i;
       int connfd;
       int clen=sizeof(struct sockaddr_in);
      
       for(i=0;i<nfds;i++)
       {
          if(events.data.fd==listenfd)
          {
              cout<<"listenfd is"<<listenfd<<endl;
              connfd=accept(listenfd,(sockaddr*)&caddr,(socklen_t*)&(clen));
              cout<<"other fd is"<<connfd<<endl;
              setnonblocking(connfd);
              ev.data.fd=connfd;
              ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
              epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
              cout<<"flagA"<<endl;  
          }
          else if(events.events&EPOLLIN)
          {
             char line[MAXLINE];
             memset(line,0,MAXLINE);
             sockfd=events.data.fd;
             cout<<"siteB"<<sockfd<<endl;
             //while(1){
             int flagB=recv(sockfd,line,MAXLINE,0);            

             if(flagB==-1){
                 if(errno==EAGAIN){
                     break;
                 }else if(errno==EPIPE) {
                     epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);
                     break;
                 }else{
                    cerr<<"other error!!!!"<<endl;
                    break;
                 }
              }else if(flagB>0)
              cout<<line<<endl;
           // }

             ev.data.fd=sockfd;
             ev.events=EPOLLOUT;
            //epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);
             cout<<"flagB"<<endl;
          }
          else if(events.events&EPOLLOUT)
          {  
              char* res="this is server1..";
              int sockfd=events.data.fd;
              
              cout<<"siteC"<<sockfd<<endl;
             // while(1){
                      int flagC=send(sockfd,res,strlen(res),0);
                     //cout<<flagC<<endl;              

                      if(flagC==-1){
                        if(errno==EAGAIN){
                           break;
                        }else if(errno==EPIPE){
                         epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);
                         break;
                        }else {
                          break;
                        }
                       }   
            
             // }
               ev.data.fd=sockfd;
               ev.events=EPOLLIN;
             // epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
              cout<<"flagC"<<endl;
          }
       }//for
   }//while
}


client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>

using namespace std;

main()
{
   int sockfd;
   int numbytes;
   char buf[100];

   memset(buf,0,100);

   struct sockaddr_in other_addr;
   sockfd=socket(AF_INET,SOCK_STREAM,0);
   
   other_addr.sin_family=AF_INET;
   other_addr.sin_port=htons(5000);
   inet_aton("127.0.0.1",&other_addr.sin_addr);
   bzero(&(other_addr.sin_zero),;

   connect(sockfd,(struct sockaddr*)&other_addr,sizeof(struct sockaddr));

   //send(sockfd,"hello!socket..",14,0);

   sleep(100000);


   char c=fgetc(stdin);

   close(sockfd);
}

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2011-08-23 19:07 |只看該作者
別這樣用 ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
去掉EPOLLOUT

關(guān)于EPOLLOUT一般可不用. 如果非要用,在讀代碼完成后使用epoll_ctl修改之( EPOLLIN|EPOLLET -> EPOLLOUT|EPOLLET ).

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2011-08-23 19:09 |只看該作者
EPOLLOUT是會(huì)一直觸發(fā)的

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2011-08-24 08:34 |只看該作者
字體斜的看起來(lái)好難受啊

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2011-08-24 08:58 |只看該作者
回復(fù) 2# youshuang

恩 多謝 ~~   說(shuō)得有道理  如果是 web服務(wù)器的話 真的沒有必要 去監(jiān)聽EPOLLOUT。。
多謝。。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2011-08-24 08:59 |只看該作者
回復(fù) 3# 凍慘鳥


    恩 多謝 ~~ 我再試試

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2011-08-24 14:42 |只看該作者
使用epoll來(lái)管理阻塞式socket是一定會(huì)出問題的:
在 if(events.data.fd==listenfd){}中應(yīng)該對(duì)listenfd循環(huán)調(diào)用accept產(chǎn)生新的連接,直到返回EAGAIN。但阻塞式socket沒法做到這一點(diǎn)。
另外,只要TCP的發(fā)送緩沖區(qū)未滿,EPOLLOUT就會(huì)一直被觸發(fā)

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2011-08-25 09:20 |只看該作者
回復(fù) 7# marxn
恩 我試試 ~~

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2011-08-28 22:10 |只看該作者
學(xué)習(xí)了。。。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP