WinSock I/O 之Select

IOServer端:

#include <iostream>
#include <WinSock2.h>
#include <tchar.h>
using namespace std;

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

#define PORT  6000
#define MSGSIZE 256

int g_iTotalConn = 0;
SOCKET g_cltSocketArr[FD_SETSIZE];

void threadProc(LPVOID lpParam)
{
	fd_set fdread;

	struct timeval tv = {1,0};
	char szMessage[MSGSIZE] = {0};

	while(true)
	{
		FD_ZERO(&fdread);

		for(int i=0;i<g_iTotalConn;i++)
		{
			FD_SET(g_cltSocketArr[i],&fdread);
		}

		int ret = select(0,&fdread,NULL,NULL,&tv);
		if(0 == ret)
			continue;

		for(int i=0;i<g_iTotalConn;i++)
		{
			if(FD_ISSET(g_cltSocketArr[i],&fdread))
			{
				ret = recv(g_cltSocketArr[i],szMessage,MSGSIZE,0);
				if(0 == ret || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))
				{
					closesocket(g_cltSocketArr[i]);
					if(i<g_iTotalConn-1)
					{
						g_cltSocketArr[i--] = g_cltSocketArr[--g_iTotalConn];
					}
				}
				cout<<"server"<<i<<" recv:"<<szMessage<<endl;
			}
			else
			{
				szMessage[ret] = '\0';
				send(g_cltSocketArr[i],szMessage,strlen(szMessage),0);
			}
		}
	}
}

void main(int argc,TCHAR*argv[])
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD( 2, 2 );

	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 )
	{
		return ;
	}

	if ( LOBYTE( wsaData.wVersion ) != 2 ||
		HIBYTE( wsaData.wVersion ) != 2 )
	{
			WSACleanup( );
			return; 
	}

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

	SOCKADDR_IN addrClient;
	addrClient.sin_addr.S_un.S_addr = htons(INADDR_ANY);
	addrClient.sin_family = AF_INET;
	addrClient.sin_port = htons(PORT);
	bind(sockSrv,(SOCKADDR*)&addrClient,sizeof(SOCKADDR));

	listen(sockSrv,5);

	SOCKADDR_IN addrConn;
	int len = sizeof(SOCKADDR);

	CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadProc,NULL,0,NULL);

	char szMessage[MSGSIZE];
	while(true)
	{
		SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrConn,&len);

// 		int nSize = recv(sockConn,szMessage,MSGSIZE,0);
// 		szMessage[nSize] = '\0';
// 		cout<<"Server recv : "<<szMessage<<endl;
// A
// 		gets(szMessage);
// 		send(sockConn,szMessage,strlen(szMessage),0);
// 
// 		closesocket(sockConn);
		g_cltSocketArr[g_iTotalConn++] = sockConn;
	}

	closesocket(sockSrv);
	WSACleanup();
}


IOClient端:

#include <iostream>
#include <WinSock2.h>
#include <tchar.h>
using namespace std;

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

#define  SERVER_ADDRESS "127.0.0.1"
#define  PORT  6000
#define  MSGSIZE 256

void main(int argc,TCHAR*argv[])
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD( 2, 2 );

	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) 
	{
		return;
	}

	if ( LOBYTE( wsaData.wVersion ) != 2 ||
		HIBYTE( wsaData.wVersion ) != 2 ) 
	{
			WSACleanup( );
			return; 
	}

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

	SOCKADDR_IN addrSrv;
	addrSrv.sin_addr.S_un.S_addr = inet_addr(SERVER_ADDRESS);
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(PORT);
	connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

	char szMessage[MSGSIZE] = {0};
	while(true)
	{
		gets(szMessage);
		send(sockClient,szMessage,strlen(szMessage),0);

		int nSize = recv(sockClient,szMessage,MSGSIZE,0);
		szMessage[nSize] = '\0';
		cout<<"Client recv: "<<szMessage<<endl;
	}

	closesocket(sockClient);
	WSACleanup();
}


 

你可能感兴趣的:(WinSock I/O 之Select)