上善若静水

while(effort=true){success++;}

   :: 首页 :: 联系 :: 聚合  :: 管理
  9 Posts :: 10 Stories :: 11 Comments :: 0 Trackbacks

留言簿(1)

搜索

  •  

积分与排名

  • 积分 - 41914
  • 排名 - 516

最新随笔

最新评论

阅读排行榜

评论排行榜

下面是使用
WSAEventSelect的一个简单的例子
#include <winsock2.h>
#include 
<stdio.h>

#pragma comment(lib, 
"ws2_32.lib")

#define LISTEN_PORT 5080
#define BUFF_SIZE   256

int main(void)
{
    WSADATA wsaData;
    
int nRet;
    SOCKET listen_sock;
    SOCKADDR_IN listen_addr;
    WSAEVENT startEvent;
    WSAEVENT clientEvent;
    SOCKET client_sock;
    
int event_num = 0;
    
int index;
    SOCKADDR_IN client_addr;
    
int client_addr_len = sizeof(client_addr);
    WSANETWORKEVENTS wsaNetworkEvents;
    WSAEVENT event_array[WSA_MAXIMUM_WAIT_EVENTS];
    SOCKET sock_array[WSA_MAXIMUM_WAIT_EVENTS];
    
char buff[BUFF_SIZE];
    
int accept_num = 0;


    nRet 
= WSAStartup(MAKEWORD(2,2), &wsaData);
    
if(0 != nRet)
    {
        printf(
"WSAStartup() failed, error:%d.\n", WSAGetLastError());
        
return 1;
    }

    listen_sock 
= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
if(INVALID_SOCKET == listen_sock)
    {
        printf(
"socket() failed, error:%d.\n", WSAGetLastError());
        
goto rettag1;
    }

    listen_addr.sin_family 
= AF_INET;
    listen_addr.sin_addr.s_addr 
= htonl(INADDR_ANY);
    listen_addr.sin_port 
= htons(LISTEN_PORT);
    nRet 
= bind(listen_sock, (SOCKADDR*)&listen_addr, sizeof(listen_addr));
    
if(nRet == SOCKET_ERROR)
    {
        printf(
"bind() failed, error:%d.\n", WSAGetLastError());
        closesocket(listen_sock);
        
goto rettag1;
    }

    nRet 
= listen(listen_sock, 5);
    
if(SOCKET_ERROR == nRet)
    {
        printf(
"listen() failed, error:%d.\n", WSAGetLastError());
        closesocket(listen_sock);
        
goto rettag1;
    }

    startEvent 
= WSACreateEvent();
    
if (startEvent == WSA_INVALID_EVENT)
    {
        printf(
"WSACreateEvent() failed, error:%d.\n", WSAGetLastError());
        closesocket(listen_sock);
        
goto rettag1;
    }

    nRet 
= WSAEventSelect(listen_sock, startEvent, FD_ACCEPT|FD_CLOSE);
    
if(SOCKET_ERROR == nRet)
    {
        printf(
"WSAEventSelect() failed, error:%d.\n", WSAGetLastError());
        WSACloseEvent(startEvent);
        closesocket(listen_sock);
        
goto rettag1;
    }

    event_array[event_num] 
= startEvent;
    sock_array[event_num] 
= listen_sock;
    
++event_num;

    
while(1)
    {
        index 
= WSAWaitForMultipleEvents(event_num, event_array, FALSE, WSA_INFINITE, FALSE);
        
if(index == WSA_WAIT_FAILED || index == WSA_WAIT_TIMEOUT)
            
continue;

        index 
-= WSA_WAIT_EVENT_0;
        
        nRet 
= WSAEnumNetworkEvents(sock_array[index], event_array[index], &wsaNetworkEvents);
        
if(SOCKET_ERROR == nRet)
        {
            printf(
"WSAEnumNetworkEvents() failed, error:%d.\n", WSAGetLastError());
            
continue;
        }

        
if(wsaNetworkEvents.lNetworkEvents & FD_ACCEPT)
        {
            
if(wsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
            {
                printf(
"FD_ACCEPT error:%d.\n", wsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT]);
                
continue;
            }

            
if(event_num >= WSA_MAXIMUM_WAIT_EVENTS)
            {
                
continue;
            }

            client_sock 
= accept(sock_array[index], (SOCKADDR*)&client_addr, &client_addr_len);
            
if(INVALID_SOCKET == client_sock)
            {
                printf(
"in accept failed, error:%d.\n", WSAGetLastError());
                
continue;
            }

            clientEvent 
= WSACreateEvent();
            
if(clientEvent == WSA_INVALID_EVENT)
            {
                printf(
"in WSACreateEvent() failed, error:%d.\n", WSAGetLastError());
                closesocket(client_sock);
                
continue;
            }

            nRet 
= WSAEventSelect(client_sock, clientEvent, FD_READ|FD_WRITE|FD_CLOSE);
            
if(SOCKET_ERROR == nRet)
            {
                printf(
"in WSAEventSelect() failed, error:%d.\n", WSAGetLastError());
                closesocket(client_sock);
                
continue;
            }

            event_array[event_num] 
= clientEvent;
            sock_array[event_num] 
= client_sock;
            
++event_num;

            
++accept_num;
        }
        
else if(wsaNetworkEvents.lNetworkEvents & FD_READ)
        {
            
if(wsaNetworkEvents.iErrorCode[FD_READ_BIT] != 0)
            {
                printf(
"FD_READ error:%d.\n", wsaNetworkEvents.iErrorCode[FD_READ_BIT]);
                
continue;                
            }

            nRet 
= recv(sock_array[index], buff, BUFF_SIZE, 0);
            
if(nRet == SOCKET_ERROR)
            {
                printf(
"recv() failed, error:%d.\n", WSAGetLastError());
                
continue;
            }
            
else if(nRet == 0)
            {
                printf(
"recv() 0.\n");
                
continue;
            }
            
else
            {
                printf(
"\t%s", buff);
            }
        }
        
else if(wsaNetworkEvents.lNetworkEvents & FD_WRITE)
        {
            
if(wsaNetworkEvents.iErrorCode[FD_WRITE_BIT] != 0)
            {
                printf(
"FD_WRITE error:%d.\n", wsaNetworkEvents.iErrorCode[FD_WRITE_BIT]);
                
continue;                
            }        
            printf(
"FD_WRITE do.\n");
        }
        
else if(wsaNetworkEvents.lNetworkEvents & FD_CLOSE)
        {
            
if(wsaNetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
            {
                printf(
"FD_CLOSE error:%d.\n", wsaNetworkEvents.iErrorCode[FD_CLOSE_BIT]);        
            }    
            printf(
"\nFD_CLOSE!\n");

            closesocket(sock_array[index]);
            WSACloseEvent(event_array[index]);

            
while(index+1 < event_num)
            {
                sock_array[index] 
= sock_array[index+1];
                event_array[index] 
= event_array[index+1];
                
++index;
            }
            
--event_num;
            
if (event_num <= accept_num)
                
break;
        }
    }

    
while(event_num > 0)
    {
        closesocket(sock_array[event_num]);
        WSACloseEvent(event_array[event_num]);
        
--event_num;
    }

rettag1:
    nRet 
= WSACleanup();
    
if(0 != nRet)
    {
        printf(
"WSACleanup() failed, error:%d.\n", WSAGetLastError());
        
return 1;
    }

    
return 0;
}

posted on 2011-08-23 17:32 上善若静水 阅读(1342) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理