谷先生你好,想请教你一个udt中select模型的问题

2009-07-21
2013-01-28
  • liulang sanmao
    liulang sanmao
    2009-07-21

    谷先生,最近用到了你写的udt,首先感谢你。 想请教你一下为什么select第一次执行时正确,后面执行就回出错。  服务器端代码如下:
    timeval tv;
    UDT::UDSET  fdsocket;
    UDT::UDSET fdtemp;
    tv.tv_sec = 1;
    tv.tv_usec = 0;

    UD_ZERO(&fdsocket);
    UD_SET(StartRecv->s, &fdsocket);
    UDT::UDSET  fdread;

        while(TRUE)
        {
            fdread=fdsocket;
            fdtemp=fdsocket;
            //cout<<"select"<<endl;
            int res = UDT::select(0, &fdread, NULL, NULL, &tv);
            //cout<<res;
            if (res>0&&(res != UDT::ERROR))
            {
                for (set<UDTSOCKET>::iterator b=fdsocket.begin(); b!=fdsocket.end(); ++b)
                {
                    if (UD_ISSET(*b, &fdread));
                    {
                        //接收到连接
                        if (*b==StartRecv->s)
                        {
                            //cout<<"b"<<*b;
                            sockaddr_storage clientaddr;
                            int addrlen = sizeof(clientaddr);

                            UDTSOCKET recver;
                            if (UDT::INVALID_SOCK == (recver = UDT::accept(StartRecv->s, (sockaddr*)&clientaddr, &addrlen)))
                            {
                                cout << "accept: " << UDT::getlasterror().getErrorMessage() << endl;
                                continue;
                            }
                            cout<<"recver"<<recver<<endl;
                            UD_SET(recver, &fdsocket);
                        }
                        else
                        {
                            //接收到数据
                            //memset(StartRecv->RecvBuffer,0,sizeof(CmdPackage));
                            char buffer[100]={0};
                            int recvnum = UDT::recv(*b, buffer , 100, 0);
                            //int recvnum = UDT::recv(*b, StartRecv->RecvBuffer , sizeof(CmdPackage), 0);
                            cout<<recvnum;
                            if (recvnum>0)
                            {
                                //cout<<"接收到数据";
                                cout<<"接收到数据"<<buffer;
                            }
    //                         else
    //                         {
    //                             UDT::close(*b);
    //                         }
                           
                        
                        }
                    }   
                }
               
            }

        }

     
    • Yunhong Gu
      Yunhong Gu
      2009-07-22

      出的什么错误? 有可能是你的recver socket被关闭了。

       
    • liulang sanmao
      liulang sanmao
      2009-07-22

      谢谢谷先生的回复,问题好像是当客户端退出后(UDT::close(client)),服务器端会在accept 那个地方挂起,当下一个客户端来连接时,就直接从accept 处开始,而没有从select函数处开始,这样就导致了select(非法的socketid)返回错误,从而使select失去作用。