select网络模式


fd_set set;

FD_ZERO(&set); /*将set清零使集合中不含任何fd*/

FD_SET(fd, &set); /*将fd加入set集合*/

FD_CLR(fd, &set); /*将fd从set集合中清除*/

FD_ISSET(fd, &set); /*在调用select()函数后,用FD_ISSET来检测fd是否在set集合中,当检测到fd在set中则返回真,否则,返回假(0)*/
// select网络模型.cpp : 定义控制台应用程序的入口点。
//

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

#pragma comment (lib,"ws2_32.lib")
#define PORT 6000
fd_set g_fdClientSock;
int clientNum = 0;
DWORD WINAPI ClinetProc(LPVOID lparam)
{
    fd_set fdRead;
    FD_ZERO(&fdRead);
    int nRet = 0;
    char *recvBuffer = new char[1024];
    if (recvBuffer==NULL)
    {
        return -1;
    }
    memset(recvBuffer, 0, sizeof(char) * 1024);
    while (TRUE)
    {
        fdRead = g_fdClientSock;
        timeval tv;
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        nRet = select(0, &fdRead, NULL, NULL, &tv);
        if (nRet!=SOCKET_ERROR)
        {
            for (int i = 0; i < g_fdClientSock.fd_count; i++)
            {
                if (FD_ISSET(g_fdClientSock.fd_array[i],&fdRead))
                {
                    memset(recvBuffer, 0, sizeof(char) * 1024);
                    nRet = recv(g_fdClientSock.fd_array[i], recvBuffer, sizeof(char) * 1024, 0);
                    if (nRet==SOCKET_ERROR)
                    {
                        cout << "接收失败" << endl;
                        closesocket(g_fdClientSock.fd_array[i]);
                        clientNum--;
                        FD_CLR(g_fdClientSock.fd_array[i],&g_fdClientSock);
                    }
                    else if (nRet==0)
                    {
                        cout << "客户端断开连接" << endl;
                        closesocket(g_fdClientSock.fd_array[i]);
                        clientNum--;
                        FD_CLR(g_fdClientSock.fd_array[i], &g_fdClientSock);
                    }
                    else
                    {
                        cout << "Server recv msg:"<<recvBuffer<< endl;
                        send(g_fdClientSock.fd_array[i], recvBuffer, strlen(recvBuffer), 0);
                    }
                }
            }
        }
        
    }
    if (recvBuffer!=NULL)
    {
        delete recvBuffer;
    }
    return 0;
}


bool InitNetEvn()
{
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        return false;
    }
    if (LOBYTE(wsa.wVersion)!=2||HIBYTE(wsa.wVersion)!=2)
    {
        WSACleanup();
        return false;
    }
    return true;

}


int main()
{
    if (!InitNetEvn())
    {
        cout << "初始化网络环境失败" << endl;
        return -1;
    }
    //使用TCP协议
    SOCKET listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    /*if (listenSock==INVALID_SOCKET)
    {
        cout << "创建套接字失败" << endl;
        return -1;
    }*/
    SOCKADDR_IN server;
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    int ret = bind(listenSock, (sockaddr *)&server, sizeof(server));
    /*if (ret==INVALID_SOCKET)
    {
        cout << "绑定失败" << endl;
        closesocket(listenSock);
        WSACleanup();
        return -1;
    }*/
    ret = listen(listenSock, 4);
    SOCKADDR_IN clientAddr;
    int nameLen = sizeof(sockaddr);
    HANDLE hTread=CreateThread(NULL, NULL, ClinetProc, NULL, NULL, NULL);
    /*if (hTread==NULL)
    {
        cout << "创建线程失败" << endl;
        return -1;
    }*/
    while (clientNum<FD_SETSIZE)
    {
        SOCKET clientSock = accept(listenSock, (sockaddr*)&clientAddr, &nameLen);

        FD_SET(clientSock,&g_fdClientSock);

        clientNum++;
    }
    closesocket(listenSock);
    WSACleanup();
    return 0;
}

 

posted @ 2017-07-05 20:41  菜鸟也有高飞的时候  阅读(617)  评论(0编辑  收藏  举报