| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1760 人关注过本帖
标题:重叠模型中WSARecv方法调用为何出错?
只看楼主 加入收藏
paerxiushi
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-3-28
收藏
 问题点数:0 回复次数:0 
重叠模型中WSARecv方法调用为何出错?
小弟做的重叠I/O模型的服务端代码如下:
WSABUF buf;
    WSAOVERLAPPED overlapped;
    OVERLAPPED over;
    int eventTotal=0;
    WSAEVENT events[WSA_MAXIMUM_WAIT_EVENTS];
    char mess[DATA_BUF];
    SOCKET client;
    DWORD recvBufLen;
    DWORD dwFlags;

    SOCKET server=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    sockaddr_in serAddr;
    serAddr.sin_family=AF_INET;
    serAddr.sin_port=htons(9050);
    serAddr.sin_addr.s_addr=INADDR_ANY;
    bind(server,(sockaddr*)&serAddr,sizeof(serAddr));
    
    listen(server,5);
    
    sockaddr_in cltAddr;
    int addrLen=sizeof(cltAddr);
    client=accept(server,(LPSOCKADDR)&cltAddr,&addrLen);
    
    printf("接受到一个新的连接:%s,%d",inet_ntoa(cltAddr.sin_addr),ntohs(cltAddr.sin_port));

    ZeroMemory(&buf,sizeof(buf));
    buf.buf=mess;
    buf.len=DATA_BUF;
    ZeroMemory(&overlapped,sizeof(overlapped));
    overlapped.hEvent=WSACreateEvent();
    
    events[eventTotal]=overlapped.hEvent;
    eventTotal++;
    if(WSARecv(client,&buf,1,&recvBufLen,&dwFlags,&overlapped,NULL)==SOCKET_ERROR)
    {
        if(WSAGetLastError()!=WSA_IO_PENDING)
        {
            printf("recv client sending failed\n");
            printf("%d",WSAGetLastError());
            /*closesocket(client);
            WSACloseEvent(events[0]);
            return;*/
        }
    }
    while (true)
    {
        int index;
        index=WSAWaitForMultipleEvents(1,&events[0],TRUE,WSA_INFINITE,FALSE);
        index=index-WSA_WAIT_EVENT_0;
        WSAResetEvent(events[index]);
        WSAGetOverlappedResult(client,&overlapped,&recvBufLen,FALSE,&dwFlags);
        if(recvBufLen==0)
        {
            printf("client close the connection\n");
            closesocket(client);
            WSACloseEvent(events[index]);
            break;
        }
        printf("接收到数据:%s",buf.buf);
                  。。。
}
closesocket(server);
当客户端连接上后,在语句“if(WSARecv(client,&buf,1,&recvBufLen,&dwFlags,&overlapped,NULL)==SOCKET_ERROR)
”出错,出错代码是10045,怎么回事呀,而且后面的WSAWaitForMultipleEvents方法也无法返回了?
搜索更多相关主题的帖子: WSARecv 模型 serAddr sin SOCKET 
2008-03-28 09:11
快速回复:重叠模型中WSARecv方法调用为何出错?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.076805 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved