#include<WinSock2.h>
#include<iostream>
#include<vector>
#include<process.h>
#include<cstdlib>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define PORT 1800
#define MAXTHREAD 10
SOCKET gLis_Socket;
vector<SOCKET> gAcc_Socket(MAXTHREAD,-1);
//SOCKET gAcc_Socket[MAXTHREAD]={-1};
short int numofThread=0;
unsigned int WINAPI Proc(void* lparam)
{
SOCKET threadSocket=(SOCKET)((int*)lparam)[0];
unsigned char msgBuf[128]={0};
unsigned char dataBuf[0xFF]={0};
char sendBuf[128]={0};
//strcpy_s(sendBuf,"hello client");
while(recv(threadSocket,(char*)msgBuf,sizeof(msgBuf),0)>0)
{
cout<<msgBuf<<endl;
if(0==strcmp((char*)msgBuf,"quit"))
break;
memset(msgBuf,0,128);
cin.getline(sendBuf,sizeof(sendBuf));
send(threadSocket,sendBuf,strlen(sendBuf),0);
}
closesocket(threadSocket);
_endthreadex(0);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
int iRet=WSAStartup(MAKEWORD(2,2),&wsaData);
if(NO_ERROR!=iRet)
{
cout<<"Error at WSAStartup()"<<endl;
WSACleanup();
return -1;
}
SOCKET listen_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==listen_socket)
{
cout<<"Error in New a Socket!"<<endl;
WSACleanup();
return -2;
}
sockaddr_in local;
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons(PORT);
if(0!=bind(listen_socket,(sockaddr*)&local,sizeof(local)))
{
cout<<"Error in Binding socket"<<endl;
WSACleanup();
return -3;
}
if(0!=listen(listen_socket,MAXTHREAD))
{
cout<<"Error in Listen"<<endl;
WSACleanup();
return -4;
}
gLis_Socket=listen_socket;
while(true)
{
while(SOCKET_ERROR==gAcc_Socket[numofThread])
{
gAcc_Socket[numofThread]=accept(gLis_Socket,NULL,NULL);
}
unsigned int dwThreadID;
unsigned long hThread;
LPVOID threadParams[]={(LPVOID)gAcc_Socket[numofThread],(LPVOID)numofThread};
hThread=_beginthreadex(NULL,NULL,Proc,threadParams,0,&dwThreadID);
if(NULL==hThread)
{
WSACleanup();
return -5;
}
else
{
numofThread++;
}
WaitForSingleObject(reinterpret_cast<HANDLE>(hThread),INFINITE);
CloseHandle((HANDLE)hThread);
}
return 0;
}
#include<iostream>
#include<vector>
#include<process.h>
#include<cstdlib>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define PORT 1800
#define MAXTHREAD 10
SOCKET gLis_Socket;
vector<SOCKET> gAcc_Socket(MAXTHREAD,-1);
//SOCKET gAcc_Socket[MAXTHREAD]={-1};
short int numofThread=0;
unsigned int WINAPI Proc(void* lparam)
{
SOCKET threadSocket=(SOCKET)((int*)lparam)[0];
unsigned char msgBuf[128]={0};
unsigned char dataBuf[0xFF]={0};
char sendBuf[128]={0};
//strcpy_s(sendBuf,"hello client");
while(recv(threadSocket,(char*)msgBuf,sizeof(msgBuf),0)>0)
{
cout<<msgBuf<<endl;
if(0==strcmp((char*)msgBuf,"quit"))
break;
memset(msgBuf,0,128);
cin.getline(sendBuf,sizeof(sendBuf));
send(threadSocket,sendBuf,strlen(sendBuf),0);
}
closesocket(threadSocket);
_endthreadex(0);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
int iRet=WSAStartup(MAKEWORD(2,2),&wsaData);
if(NO_ERROR!=iRet)
{
cout<<"Error at WSAStartup()"<<endl;
WSACleanup();
return -1;
}
SOCKET listen_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==listen_socket)
{
cout<<"Error in New a Socket!"<<endl;
WSACleanup();
return -2;
}
sockaddr_in local;
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons(PORT);
if(0!=bind(listen_socket,(sockaddr*)&local,sizeof(local)))
{
cout<<"Error in Binding socket"<<endl;
WSACleanup();
return -3;
}
if(0!=listen(listen_socket,MAXTHREAD))
{
cout<<"Error in Listen"<<endl;
WSACleanup();
return -4;
}
gLis_Socket=listen_socket;
while(true)
{
while(SOCKET_ERROR==gAcc_Socket[numofThread])
{
gAcc_Socket[numofThread]=accept(gLis_Socket,NULL,NULL);
}
unsigned int dwThreadID;
unsigned long hThread;
LPVOID threadParams[]={(LPVOID)gAcc_Socket[numofThread],(LPVOID)numofThread};
hThread=_beginthreadex(NULL,NULL,Proc,threadParams,0,&dwThreadID);
if(NULL==hThread)
{
WSACleanup();
return -5;
}
else
{
numofThread++;
}
WaitForSingleObject(reinterpret_cast<HANDLE>(hThread),INFINITE);
CloseHandle((HANDLE)hThread);
}
return 0;
}