Code
//这些事情属于那低回报的80%的时间里面的事情。那么我们应该尽可能的减少花费在这些事情上的时间,这才是我们应该从80/20定律中得出的结论。
// Module Name: eventselect.cpp
//
// Description:
//
// This sample illustrates how to develop a simple echo server Winsock
// application using the WSAEventSelect() I/O model. This sample is
// implemented as a console-style application and simply prints
// messages when connections are established and removed from the server.
// The application listens for TCP connections on port 5150 and accepts them
// as they arrive. When this application receives data from a client, it
// simply echos (this is why we call it an echo server) the data back in
// it's original form until the client closes the connection.
//
// Compile:
//
// cl -o eventselect eventselect.cpp ws2_32.lib
//
// Command Line Options:
//
// eventselect.exe
//
// Note: There are no command line options for this sample.
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#define PORT 5150
#define DATA_BUFSIZE 8192
//SOCKET 信息
typedef struct _SOCKET_INFORMATION {
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Event);
DWORD EventTotal = 0;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
LPSOCKET_INFORMATION SocketArray[WSA_MAXIMUM_WAIT_EVENTS];
void main(void)
{
SOCKET Listen;
SOCKET Accept;
SOCKADDR_IN InternetAddr;
DWORD Event;
WSANETWORKEVENTS NetworkEvents;
WSADATA wsaData;
DWORD Ret;
DWORD Flags;
DWORD RecvBytes;
DWORD SendBytes;
if ((Ret = WSAStartup(0x0202, &wsaData)) != 0)
{
printf("WSAStartup() failed with error %d\n", Ret);
return;
}
if ((Listen = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("socket() failed with error %d\n", WSAGetLastError());
return;
}
CreateSocketInformation(Listen);
if (WSAEventSelect(Listen, EventArray[EventTotal - 1], FD_ACCEPT|FD_CLOSE) == SOCKET_ERROR)
{
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
return;
}
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(Listen, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf("bind() failed with error %d\n", WSAGetLastError());
return;
}
if (listen(Listen, 5))
{
printf("listen() failed with error %d\n", WSAGetLastError());
return;
}
while(TRUE)
{
// Wait for one of the sockets to receive I/O notification and
if ((Event = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE,
WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)
{
printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
return;
}
if (WSAEnumNetworkEvents(SocketArray[Event - WSA_WAIT_EVENT_0]->Socket, EventArray[Event -
WSA_WAIT_EVENT_0], &NetworkEvents) == SOCKET_ERROR)
{
printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
return;
}
if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
{
if (NetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
{
printf("FD_ACCEPT failed with error %d\n", NetworkEvents.iErrorCode[FD_ACCEPT_BIT]);
break;
}
if ((Accept = accept(SocketArray[Event - WSA_WAIT_EVENT_0]->Socket, NULL, NULL)) == INVALID_SOCKET)
{
printf("accept() failed with error %d\n", WSAGetLastError());
break;
}
if (EventTotal > WSA_MAXIMUM_WAIT_EVENTS)
{
printf("Too many connections - closing socket.\n");
closesocket(Accept);
break;
}
CreateSocketInformation(Accept);
if (WSAEventSelect(Accept, EventArray[EventTotal - 1], FD_READ|FD_WRITE|FD_CLOSE) == SOCKET_ERROR)
{
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
return;
}
printf("Socket %d connected\n", Accept);
}
// Try to read and write data to and from the data buffer if read and write events occur.
if (NetworkEvents.lNetworkEvents & FD_READ ||
NetworkEvents.lNetworkEvents & FD_WRITE)
{
if (NetworkEvents.lNetworkEvents & FD_READ &&
NetworkEvents.iErrorCode[FD_READ_BIT] != 0)
{
printf("FD_READ failed with error %d\n", NetworkEvents.iErrorCode[FD_READ_BIT]);
break;
}
if (NetworkEvents.lNetworkEvents & FD_WRITE &&
NetworkEvents.iErrorCode[FD_WRITE_BIT] != 0)
{
printf("FD_WRITE failed with error %d\n", NetworkEvents.iErrorCode[FD_WRITE_BIT]);
break;
}
LPSOCKET_INFORMATION SocketInfo = SocketArray[Event - WSA_WAIT_EVENT_0];
// Read data only if the receive buffer is empty.
if (SocketInfo->BytesRECV == 0)
{
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = DATA_BUFSIZE;
Flags = 0;
if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
&Flags, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
return;
}
}
else
{
SocketInfo->BytesRECV = RecvBytes;
}
}
// Write buffer data if it is available.
if (SocketInfo->BytesRECV > SocketInfo->BytesSEND)
{
SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->BytesSEND;
SocketInfo->DataBuf.len = SocketInfo->BytesRECV - SocketInfo->BytesSEND;
if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
return;
}
// A WSAEWOULDBLOCK error has occured. An FD_WRITE event will be posted
// when more buffer space becomes available
}
else
{
SocketInfo->BytesSEND += SendBytes;
if (SocketInfo->BytesSEND == SocketInfo->BytesRECV)
{
SocketInfo->BytesSEND = 0;
SocketInfo->BytesRECV = 0;
}
}
}
}
if (NetworkEvents.lNetworkEvents & FD_CLOSE)
{
if (NetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
{
printf("FD_CLOSE failed with error %d\n", NetworkEvents.iErrorCode[FD_CLOSE_BIT]);
break;
}
printf("Closing socket information %d\n", SocketArray[Event - WSA_WAIT_EVENT_0]->Socket);
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
}
}
return;
}
BOOL CreateSocketInformation(SOCKET s)
{
LPSOCKET_INFORMATION SI;
if ((EventArray[EventTotal] = WSACreateEvent()) == WSA_INVALID_EVENT)
{
printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());
return FALSE;
}
if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
printf("GlobalAlloc() failed with error %d\n", GetLastError());
return FALSE;
}
// Prepare SocketInfo structure for use.
SI->Socket = s;
SI->BytesSEND = 0;
SI->BytesRECV = 0;
SocketArray[EventTotal] = SI;
EventTotal++;
return(TRUE);
}
void FreeSocketInformation(DWORD Event)
{
LPSOCKET_INFORMATION SI = SocketArray[Event];
DWORD i;
closesocket(SI->Socket);
GlobalFree(SI);
WSACloseEvent(EventArray[Event]);
// Squash the socket and event arrays
for (i = Event; i < EventTotal; i++)
{
EventArray[i] = EventArray[i + 1];
SocketArray[i] = SocketArray[i + 1];
}
EventTotal--;
}
//这些事情属于那低回报的80%的时间里面的事情。那么我们应该尽可能的减少花费在这些事情上的时间,这才是我们应该从80/20定律中得出的结论。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- #define LOC_PORT 5678
- #define LOC_ADDR "127.0.0.1"
- class CSocketObject
- {
- public:
- CSocketObject(){WSAStartup(MAKEWORD(2,2), &m_wsaData);}
- virtual ~CSocketObject(){WSACleanup();}
- public:
- virtual void Run() = 0;
- protected:
- WSADATA m_wsaData;
- };
- class CTcpServer : public CSocketObject
- {
- public:
- virtual void Run();
- private:
- typedef struct
- {
- WSAEVENT event;
- SOCKET sock;
- }EVENT_T;
- list
m_listClient; - static WSAEVENT& DoGetEvent(EVENT_T& ev){return ev.event;}
- };
- void CTcpServer::Run()
- {
- SOCKET tSock = socket(AF_INET, SOCK_STREAM, 0);
- char cBuf[1024];
- SOCKADDR_IN tAddr, tComeinAddr;
- tAddr.sin_family = AF_INET;
- tAddr.sin_port = htons(LOC_PORT);
- tAddr.sin_addr.s_addr = inet_addr(LOC_ADDR);
- _ASSERT(bind(tSock,(SOCKADDR*)&tAddr, sizeof(tAddr)) == 0);
- WSAEVENT hWSAEvent = WSACreateEvent();
- WSAEventSelect(tSock, hWSAEvent, FD_ACCEPT);
- _ASSERT(listen(tSock, 5) == 0);
- EVENT_T ev = {hWSAEvent, tSock};
- m_listClient.push_back(ev);
- WSAEVENT events[64];
- for (;;)
- {
- transform(m_listClient.begin(), m_listClient.end(), events, DoGetEvent);
- int dwIndex = WSAWaitForMultipleEvents(m_listClient.size(), events, false, WSA_INFINITE, false);
- list
::iterator iter = m_listClient.begin(); - int iPos = 0;
- while (iPos++ != dwIndex - WSA_WAIT_EVENT_0)++iter;
- WSANETWORKEVENTS tNetEvents;
- WSAEnumNetworkEvents(iter->sock, iter->event, &tNetEvents);
- if (FD_ACCEPT & tNetEvents.lNetworkEvents)
- {
- if (0 != tNetEvents.iErrorCode[FD_ACCEPT_BIT])
- {
- std::cout<<"accept error occured\n";
- continue;
- }
- else
- {
- int dwAddrLen = sizeof(tComeinAddr);
- SOCKET tSockIn = accept(tSock, (sockaddr*)&tComeinAddr, &dwAddrLen);
- ev.event = WSACreateEvent();
- WSAEventSelect(tSockIn, ev.event, FD_READ | FD_CLOSE);
- ev.sock = tSockIn;
- m_listClient.push_back(ev);
- cout << "new come in..." << inet_ntoa(tComeinAddr.sin_addr) << "\n";
- send(tSockIn, "You are welcome", strlen("You are welcome"), 0);
- }
- }
- else if (FD_READ & tNetEvents.lNetworkEvents)
- {
- if (0 != tNetEvents.iErrorCode[FD_READ_BIT])
- {
- std::cout<<"read error occured\n";
- continue;
- }
- else
- {
- int dwRet = recv(iter->sock, cBuf, 1024, 0);
- if (SOCKET_ERROR == dwRet)
- {
- }
- else
- {
- cout << "client...data: ";
- for (int n = 0; n != dwRet; ++n)
- {
- cout << "0x" << hex << (int)cBuf[n] << " ";
- }
- cout << endl;
- }
- }
- }
- else if (FD_CLOSE & tNetEvents.lNetworkEvents)
- {
- int dwNameLen = sizeof(tComeinAddr);
- getpeername(iter->sock, (sockaddr*)&tComeinAddr, &dwNameLen);
- closesocket(iter->sock);
- WSACloseEvent(iter->event);
- m_listClient.erase(iter);
- cout << "client..." << inet_ntoa(tComeinAddr.sin_addr) << " quit\n";
- }
- }
- }
- int _tmain(int argc, TCHAR *argv[])
- {
- CSocketObject *poSockServer = new CTcpServer;
- poSockServer->Run();
- delete poSockServer;
- return 0;
- }
// Module Name: eventselect.cpp
//
// Description:
//
// This sample illustrates how to develop a simple echo server Winsock
// application using the WSAEventSelect() I/O model. This sample is
// implemented as a console-style application and simply prints
// messages when connections are established and removed from the server.
// The application listens for TCP connections on port 5150 and accepts them
// as they arrive. When this application receives data from a client, it
// simply echos (this is why we call it an echo server) the data back in
// it's original form until the client closes the connection.
//
// Compile:
//
// cl -o eventselect eventselect.cpp ws2_32.lib
//
// Command Line Options:
//
// eventselect.exe
//
// Note: There are no command line options for this sample.
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#define PORT 5150
#define DATA_BUFSIZE 8192
//SOCKET 信息
typedef struct _SOCKET_INFORMATION {
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Event);
DWORD EventTotal = 0;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
LPSOCKET_INFORMATION SocketArray[WSA_MAXIMUM_WAIT_EVENTS];
void main(void)
{
SOCKET Listen;
SOCKET Accept;
SOCKADDR_IN InternetAddr;
DWORD Event;
WSANETWORKEVENTS NetworkEvents;
WSADATA wsaData;
DWORD Ret;
DWORD Flags;
DWORD RecvBytes;
DWORD SendBytes;
if ((Ret = WSAStartup(0x0202, &wsaData)) != 0)
{
printf("WSAStartup() failed with error %d\n", Ret);
return;
}
if ((Listen = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("socket() failed with error %d\n", WSAGetLastError());
return;
}
CreateSocketInformation(Listen);
if (WSAEventSelect(Listen, EventArray[EventTotal - 1], FD_ACCEPT|FD_CLOSE) == SOCKET_ERROR)
{
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
return;
}
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(Listen, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf("bind() failed with error %d\n", WSAGetLastError());
return;
}
if (listen(Listen, 5))
{
printf("listen() failed with error %d\n", WSAGetLastError());
return;
}
while(TRUE)
{
// Wait for one of the sockets to receive I/O notification and
if ((Event = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE,
WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)
{
printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
return;
}
if (WSAEnumNetworkEvents(SocketArray[Event - WSA_WAIT_EVENT_0]->Socket, EventArray[Event -
WSA_WAIT_EVENT_0], &NetworkEvents) == SOCKET_ERROR)
{
printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
return;
}
if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
{
if (NetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
{
printf("FD_ACCEPT failed with error %d\n", NetworkEvents.iErrorCode[FD_ACCEPT_BIT]);
break;
}
if ((Accept = accept(SocketArray[Event - WSA_WAIT_EVENT_0]->Socket, NULL, NULL)) == INVALID_SOCKET)
{
printf("accept() failed with error %d\n", WSAGetLastError());
break;
}
if (EventTotal > WSA_MAXIMUM_WAIT_EVENTS)
{
printf("Too many connections - closing socket.\n");
closesocket(Accept);
break;
}
CreateSocketInformation(Accept);
if (WSAEventSelect(Accept, EventArray[EventTotal - 1], FD_READ|FD_WRITE|FD_CLOSE) == SOCKET_ERROR)
{
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
return;
}
printf("Socket %d connected\n", Accept);
}
// Try to read and write data to and from the data buffer if read and write events occur.
if (NetworkEvents.lNetworkEvents & FD_READ ||
NetworkEvents.lNetworkEvents & FD_WRITE)
{
if (NetworkEvents.lNetworkEvents & FD_READ &&
NetworkEvents.iErrorCode[FD_READ_BIT] != 0)
{
printf("FD_READ failed with error %d\n", NetworkEvents.iErrorCode[FD_READ_BIT]);
break;
}
if (NetworkEvents.lNetworkEvents & FD_WRITE &&
NetworkEvents.iErrorCode[FD_WRITE_BIT] != 0)
{
printf("FD_WRITE failed with error %d\n", NetworkEvents.iErrorCode[FD_WRITE_BIT]);
break;
}
LPSOCKET_INFORMATION SocketInfo = SocketArray[Event - WSA_WAIT_EVENT_0];
// Read data only if the receive buffer is empty.
if (SocketInfo->BytesRECV == 0)
{
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = DATA_BUFSIZE;
Flags = 0;
if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
&Flags, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
return;
}
}
else
{
SocketInfo->BytesRECV = RecvBytes;
}
}
// Write buffer data if it is available.
if (SocketInfo->BytesRECV > SocketInfo->BytesSEND)
{
SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->BytesSEND;
SocketInfo->DataBuf.len = SocketInfo->BytesRECV - SocketInfo->BytesSEND;
if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
return;
}
// A WSAEWOULDBLOCK error has occured. An FD_WRITE event will be posted
// when more buffer space becomes available
}
else
{
SocketInfo->BytesSEND += SendBytes;
if (SocketInfo->BytesSEND == SocketInfo->BytesRECV)
{
SocketInfo->BytesSEND = 0;
SocketInfo->BytesRECV = 0;
}
}
}
}
if (NetworkEvents.lNetworkEvents & FD_CLOSE)
{
if (NetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
{
printf("FD_CLOSE failed with error %d\n", NetworkEvents.iErrorCode[FD_CLOSE_BIT]);
break;
}
printf("Closing socket information %d\n", SocketArray[Event - WSA_WAIT_EVENT_0]->Socket);
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
}
}
return;
}
BOOL CreateSocketInformation(SOCKET s)
{
LPSOCKET_INFORMATION SI;
if ((EventArray[EventTotal] = WSACreateEvent()) == WSA_INVALID_EVENT)
{
printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());
return FALSE;
}
if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
printf("GlobalAlloc() failed with error %d\n", GetLastError());
return FALSE;
}
// Prepare SocketInfo structure for use.
SI->Socket = s;
SI->BytesSEND = 0;
SI->BytesRECV = 0;
SocketArray[EventTotal] = SI;
EventTotal++;
return(TRUE);
}
void FreeSocketInformation(DWORD Event)
{
LPSOCKET_INFORMATION SI = SocketArray[Event];
DWORD i;
closesocket(SI->Socket);
GlobalFree(SI);
WSACloseEvent(EventArray[Event]);
// Squash the socket and event arrays
for (i = Event; i < EventTotal; i++)
{
EventArray[i] = EventArray[i + 1];
SocketArray[i] = SocketArray[i + 1];
}
EventTotal--;
}