EventSelect网络模式

#include "stdafx.h"
#include<WinSock2.h>
#include<windows.h>
#include<iostream>
using namespace std;

#pragma comment (lib,"ws2_32.lib")
#define PORT 6000

SOCKET ArrSocket[1024] = { 0 };
WSAEVENT ArrEvent[1024] = { 0 };
DWORD dwTotal = 0;
DWORD dwIndex = 0;
int ClientNums = 0;            //表示有多少个客户端
bool InitNetEvn()
{
    WSADATA wsa = {0};
    if (WSAStartup(MAKEWORD(2,2),&wsa)!=0)
    {
        cout << "初始化网络环境失败" << endl;
        return false;
    }
    if (LOBYTE(wsa.wVersion)!=2||HIBYTE(wsa.wVersion)!=2)
    {
        cout << "网络环境的版本号不正确" << endl;
        WSACleanup();
        return false;
    }
    return true;
}

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    cout << "进入客户服务程序" << endl;
    char buf[1024] = { 0 };
    SOCKET sockClient = INVALID_SOCKET;
    WSANETWORKEVENTS netWorkEvent = { 0 };
    while (TRUE)
    {
        dwIndex = WSAWaitForMultipleEvents(dwTotal, ArrEvent, FALSE, 100, FALSE);
        if (dwIndex==WSA_WAIT_TIMEOUT)
        {
            continue;
        }
        WSAEnumNetworkEvents(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], ArrEvent[dwIndex - WSA_WAIT_EVENT_0],
            &netWorkEvent);
        if (netWorkEvent.lNetworkEvents&FD_ACCEPT)
        {
            if (netWorkEvent.iErrorCode[FD_ACCEPT_BIT]!=0)
            {
                continue;
            }
            sockClient = accept(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], NULL, NULL);
            if (sockClient==INVALID_SOCKET)
            {
                continue;
            }
            cout << "接受客户端请求" << endl;
            WSAEVENT newEvent = WSACreateEvent();
            WSAEventSelect(sockClient, newEvent, FD_READ | FD_WRITE | FD_CLOSE);
            int i;
            for (i=0;i<dwTotal;i++)
            {
                if (ArrSocket[i]==0)
                {
                    ArrSocket[i] = sockClient;
                    ArrEvent[i] = newEvent;
                    break;
                }
            }
            if (i==dwTotal)
            {
                ArrSocket[dwTotal] = sockClient;
                ArrEvent[dwTotal] = newEvent;
                ++dwTotal;
                ++ClientNums;
            }
            
        }
        if (netWorkEvent.lNetworkEvents&FD_READ)
        {
            if (netWorkEvent.iErrorCode[FD_READ_BIT]!=0)
            {
                continue;
            }
            recv(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], buf, sizeof(buf), 0);
            printf("Server Recv:%s\n", buf);
            send(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], buf, strlen(buf), 0);
        }
        if (netWorkEvent.lNetworkEvents&FD_WRITE)
        {
            if (netWorkEvent.iErrorCode[FD_WRITE_BIT]!=0)
            {
                continue;
            }
            printf("send something\n");
        }
        if (netWorkEvent.lNetworkEvents&FD_CLOSE)
        {
            if (netWorkEvent.iErrorCode[FD_CLOSE_BIT]!=0)
            {
                continue;
            }
            closesocket(ArrSocket[dwIndex - WSA_WAIT_EVENT_0]); 
            ArrSocket[dwIndex - WSA_WAIT_EVENT_0] = 0;
        }
    }
}

int main()
{
    if (!InitNetEvn())
    {
        return -1;
    }
    cout << "初始化成功" << endl;
    SOCKET socketListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (socketListen==INVALID_SOCKET)
    {
        cout << "创建套接字失败" << endl;
        return -1;
    }
    cout << "创建套接字成功" << endl;
    SOCKADDR_IN si;
    si.sin_addr.S_un.S_addr = INADDR_ANY;
    si.sin_family = AF_INET;
    si.sin_port = htons(PORT);
    int ret = bind(socketListen, (sockaddr*)&si, sizeof(sockaddr));
    if (ret==SOCKET_ERROR)
    {
        cout << "套接字绑定失败"<<endl;
        return -1;
    }
    cout << "套接字绑定成功" << endl;

    WSAEVENT listenEvent = WSACreateEvent();
    WSAEventSelect(socketListen, listenEvent, FD_CLOSE | FD_ACCEPT);
    HANDLE hThread= CreateThread(NULL, NULL, ThreadProc, NULL, NULL,NULL);
    if (hThread==NULL)
    {
        cout << "创建线程失败" << endl;
        closesocket(socketListen);
        WSACleanup();
        return -1;
    }
    ret = listen(socketListen, SOMAXCONN);
    if (ret==SOCKET_ERROR)
    {
        cout << "监听失败" << endl;
        return -1;
    }
    cout << "监听成功" << endl;
    ArrSocket[dwTotal] = socketListen;
    ArrEvent[dwTotal] = listenEvent;
    ++dwTotal;
    system("pause");
    if (socketListen!=INVALID_SOCKET)
    {
        closesocket(socketListen);
    }
    WSACleanup();
    return 0;
}

 

posted @ 2017-07-06 15:52  菜鸟也有高飞的时候  阅读(605)  评论(0编辑  收藏  举报