WSAAsyncSelect应用
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <vcl.h>
#include <winsock2.h>
#include <cassert>
#include <map>
#include <list>
#include "Datastruct.h"
using namespace std;
#pragma comment(lib,"ws2_32.lib")
namespace Async
{
#ifndef __BORLANDC__
typedef void (*receive_handler)(Data *pd);
typedef void (*error_handler)(int errcode);
typedef void (*close_handler)(SOCKET s);
typedef void (*connect_handler)(SOCKET s);
#else
typedef void __fastcall (__closure* receive_handler)(Data *pd);
typedef void __fastcall (__closure *error_handler)(int errcode);
typedef void __fastcall (__closure *close_handler)(SOCKET s);
typedef void __fastcall (__closure *connect_handler)(SOCKET s);
#endif
class AsyncSocket
{
static const int WM_SOCKET=WM_USER+109;
static map<SOCKET,list<Data*> > sMapOfComm;
static WNDPROC sOldProc;
static bool bWrite;//可写
static receive_handler sReceviceProc;
static error_handler sErrorProc;
static close_handler sCloseProc;
static connect_handler sConnectProc;
static int Send(SOCKET s)
{
map<SOCKET,list<Data*> >::iterator it=sMapOfComm.find(s);
if(it!=sMapOfComm.end())
{
for(list<Data*>::iterator itlist=it->second.begin();itlist!=it->second.end();
)
{
auto_str str;
(*itlist)->getstr(str);
int ret=::send(s,str.cstr(),str.length(),0);
if(ret==SOCKET_ERROR)//
{
if((ret=WSAGetLastError())!=WSAEWOULDBLOCK)
return 0;
else
{
bWrite=false;
return WSAEWOULDBLOCK;//缓冲区满了或对方接收缓冲区满了
}
}
else
{
delete (*itlist);
itlist=it->second.erase(itlist);
}
}
return 1;
}
return 0;
}
static LRESULT CALLBACK NewWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
if(msg==WM_SOCKET)
{
SOCKET s=(SOCKET)wParam;
if(WSAGETSELECTERROR(lParam))
{
sErrorProc(WSAGetLastError());
::closesocket(s);
return 0;
}
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
{
SOCKET sClient=::accept(s,NULL,NULL);
::WSAAsyncSelect(sClient,hwnd,WM_SOCKET,FD_READ|FD_WRITE|FD_CLOSE);
}
break;
case FD_CONNECT:
{
sConnectProc(s);
}
break;
case FD_READ:
{
DataHead dh;
dh.s=s;
&n
补充:软件开发 , 其他 ,