#ifndef Globle_H
#define Globle_H
#include
#include
#include
#include
using namespace std;
#define BUFFER_SIZE 64
#define PORT 4567
char Data[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CRITICAL_SECTION csGeneralData;
CRITICAL_SECTION csShowMsg;
int Rand(unsigned int n)
{
// RAND_MAX 是rand()能生成的最大值
return rand()%(n==0? RAND_MAX : n);
}
int ( *pFun)(unsigned int) = &Rand;
// 生成一个随机序列
void GetData(char *Buf)
{
::EnterCriticalSection(&csGeneralData);
random_shuffle(Data, Data+36, pFun); // 郁闷,VC6标准支持不好,只能这样写
memcpy(Buf, Data, 20);
Buf[20] = '\0';
::LeaveCriticalSection(&csGeneralData);
}
void ShowMsg(const char *pMsg)
{
if(pMsg == NULL) return;
EnterCriticalSection(&csShowMsg);
cout = WSA_MAXIMUM_WAIT_EVENTS)
{
::LeaveCriticalSection(&csOpArray);
return;
}
OpData.Buffer_Flag[OpInfo[dwIndex].dwIndex] = 0;
WSAEVENT hEvent = Event[dwIndex];
::WSACloseEvent(hEvent);
Event[dwIndex] = Event[EventTotal-1];
Event[EventTotal-1] = NULL;
OpInfo[dwIndex] = OpInfo[EventTotal-1];
ZeroMemory(&OpInfo[EventTotal-1], sizeof(OVERLAPPEDINFO));
EventTotal--;
::LeaveCriticalSection(&csOpArray);
}
DWORD WINAPI ConnThread(LPVOID lpParam)
{
char Msg[1024] = "";
ShowMsg("ConnThread start...");
DWORD dwIndex;
DWORD dwRecv = 0;
DWORD dwFlag = 0;
while(!bExit)
{
SOCKET Client = accept(Listen, NULL, NULL);
if(Client == SOCKET_ERROR)
{
sprintf(Msg, "Accept error %d", ::WSAGetLastError());
ShowMsg(Msg);
continue;
}
sprintf(Msg, "Socket %d connected...", Client);
ShowMsg(Msg);
if( !AddArray(Client, dwIndex) )
{
ShowMsg("Too many connections, can't do the more request!");
closesocket(Client);
continue;
}
}
ShowMsg("ConnThread exit...");
return 0;
}
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
char Buf[BUFFER_SIZE] = {0};
char Msg[1024] = "";
ShowMsg("WorkerThread start...");
DWORD dwCode;
DWORD dwIndex = 0;
DWORD dwTransfer = 0;
DWORD dwFlag = 0;
while(!bExit)
{
if(EventTotal buf, dwTransfer);
Buf[dwTransfer] = '\0';
sprintf(Msg, "Received data from socket %d: %s", OpInfo[dwIndex].Socket, Buf);
ShowMsg(Msg);
OpInfo[dwIndex].IO_Flag = NEXT_IO_READ;
GetData(OpInfo[dwIndex].wsaBuf->buf);
DWORD dwSend = 0;
// 最好别注释下面3句代码
// WSAEVENT hEvent = OpInfo[dwIndex].pOverlapped->hEvent;
// ZeroMemory(OpInfo[dwIndex].pOverlapped, sizeof(OVERLAPPED));
// OpInfo[dwIndex].pOverlapped->hEvent = hEvent;
dwCode = ::WSASend(OpInfo[dwIndex].Socket, OpInfo[dwIndex].wsaBuf, 1, &dwSend, dwFlag,
OpInfo[dwIndex].pOverlapped, NULL);
if(dwCode == SOCKET_ERROR && ::WSAGetLastError() != WSA_IO_PENDING)
{
dwCode = ::WSAGetLastError();
sprintf(Msg, "Do the WSARecv error for socket %d, error code %d",
OpInfo[dwIndex].Socket, dwCode);
ShowMsg(Msg);
// if(dwCode == WSA_IO_PENDING || dwCode == WSAECONNRESET)
// continue;
closesocket(OpInfo[dwIndex].Socket);
::CompressArray(dwIndex);
}
}
else // NEXT_IO_READ
{
sprintf(Msg, "Sended data to socket %d: %s", OpInfo[dwIndex].Socket, OpInfo[dwIndex].wsaBuf->buf);
ShowMsg(Msg);
OpInfo[dwIndex].IO_Flag = NEXT_IO_SEND;
DWORD dwRecv = 0;
ZeroMemory(OpInfo[dwIndex].wsaBuf->buf, OpInfo[dwIndex].wsaBuf->len);
// 最好别注释下面3句代码
// WSAEVENT hEvent = OpInfo[dwIndex].pOverlapped->hEvent;
// ZeroMemory(OpInfo[dwIndex].pOverlapped, sizeof(OVERLAPPED));
// OpInfo[dwIndex].pOverlapped->hEvent = hEvent;
dwCode = ::WSARecv(OpInfo[dwIndex].Socket, OpInfo[dwIndex].wsaBuf, 1, &dwRecv, &dwFlag,
OpInfo[dwIndex].pOverlapped, NULL);
if(dwCode == SOCKET_ERROR && ::WSAGetLastError() != WSA_IO_PENDING)
{
dwCode = ::WSAGetLastError();
sprintf(Msg, "Do the WSARecv error for socket %d, error code %d",
OpInfo[dwIndex].Socket, dwCode);
ShowMsg(Msg);
// if(dwCode == WSA_IO_PENDING || dwCode == WSAECONNRESET)
// continue;
closesocket(OpInfo[dwIndex].Socket);
::CompressArray(dwIndex);
}
}
}
}
ShowMsg("WorkerThread exit...");
return 0;
}
int main()
{
int i = 0;
DWORD dwCode;
WSADATA wsaData;
HANDLE hThread = NULL;
Init();
dwCode = WSAStartup(MAKEWORD(2,2), &wsaData);
if(dwCode != 0)
{
printf("\nCan't find find a usable WinSock DLL");
goto EXIT;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
{
printf("\nCan't find the socket version required.");
goto EXIT;
}
Listen = ::WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
// Listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // the smame
if(Listen == INVALID_SOCKET)
{
cout
1659628745
查看更多评论