#include
#include
#pragma comment(lib,"ws2_32.lib")
#pragma once
class casynchandler
virtual ~casynchandler()
virtual
int acceptnotify( socket hsocket, const
char *strclientip, unsigned short usclientport )= 0;
};class casynch_event_handler
/* confirm that the winsock dll supports 2.2.*/
/* note that if the dll supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* requested. */
if ( lobyte( wsadata.wversion ) != 2 ||
hibyte( wsadata.wversion ) != 2 )
return true;
};bool clearsocket();};
///*************** cpp*****************//
#include "stdafx.h"
#include "asynch_event_handler.h"
#include
using
namespace std;
casynch_event_handler::casynch_event_handler(void)
casynch_event_handler::~casynch_event_handler(void)
int casynch_event_handler::start(casynchandler * phevent, uint unport)
initsocket();
m_peventhandler = phevent;
struct sockaddr_in serveraddress;
int err;
m_hlistensocket = socket(af_inet, sock_stream, 0);
if( invalid_socket == m_hlistensocket )
memset(&serveraddress, 0, sizeof(serveraddress));
serveraddress.sin_family = af_inet;
serveraddress.sin_addr.s_addr = 0;
serveraddress.sin_port = htons( unport );
err = bind(m_hlistensocket,
(struct sockaddr *)&serveraddress,
sizeof(serveraddress)
);if( err == socket_error )
err = listen( m_hlistensocket, somaxconn );
if( err == socket_error )
m_hexitthread = createevent( null, true, false, null );
m_hiothread = (handle)
_beginthreadex(
null,
0,acceptthread,
this,
0,0 );
return 0;
}int casynch_event_handler::stop(void)
closesocket( m_hlistensocket );
m_hlistensocket = invalid_socket;
setevent( m_hexitthread );
if( wait_timeout == waitforsingleobject( m_hiothread, 10000 ) )
closehandle( m_hexitthread );
closehandle( m_hiothread );
clearsocket();
return 0;
}unsigned int casynch_event_handler::acceptthreadproc()
; hwaitall[0] = m_hexitthread;
hwaitall[1] = wsacreateevent();
wsaeventselect(
m_hlistensocket,
hwaitall[1],
fd_accept );
int nwaitcounts = 2;
while( true )
dword index = wt - wsa_wait_event_0;
if( index == 1 )
else}}
}else
if( wt == wait_io_completion )
else
}return 0;
}unsigned int __stdcall casynch_event_handler::acceptthread(void * lpparam)
int casynch_event_handler::onaccept()
else
nvalue = 16384;
if( socket_error == setsockopt( acceptsocket, sol_socket ,so_sndbuf, (char *)&nvalue, nlen ) )
if( socket_error == setsockopt( acceptsocket, sol_socket ,so_rcvbuf, (char *)&nvalue, nlen ) )
m_peventhandler->acceptnotify( acceptsocket, inet_ntoa( clientaddress.sin_addr ), ntohs( clientaddress.sin_port ) );
return 0;}}
封裝與解封裝
封裝與解封裝 封裝動作 從高層往低層依次封裝,在每一層使用特定的協議,對資料進行處理,在資料前新增特定的協議報頭。封裝原則 1 每一層在上一層資料前新增協議報頭 2 新增完協議報頭的整體,就是該層的pdu 3 每一層的pdu對於下一層來說就是上層資料 每一層的上層資料就是上層的pdu pdu 協議資...
pdo mysql 封裝 pdo mysql 封裝
namespace core 資料庫操作類 author chenyuwen class database 鏈結目標物件 var string protected db target null 使用者名稱 var string protected user name null 密碼 var stri...
jquery封裝之 css封裝
我之間我的部落格裡面有提到,自定義的庫和jquery庫是水火不相容的,其實在我學習了css封裝後,我算是大徹大悟。在前面的時候我提到,獲取節點,要用到繁瑣的getid,getclass,gettagname.今天學習了css封裝,我們也可以簡化js 設定 我自己定義的base.js 前台呼叫 var...