资源简介

一个VC++ socket 异步通信类

资源截图

代码片段和文件信息

/*
** Copyright (C) Roman Ukhov 2000
** e-mail: r_ukhov@yahoo.com
** http://rukhov.newmail.ru
*/

#include “http_server.hpp“
#include “async_socket.hpp“

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace network
{
async_socket::async_socket(unsigned long _timeout):
the_socket(INVALID_SOCKET)
hEvent(WSA_INVALID_EVENT)
hStopEvent(WSA_INVALID_EVENT)
l_events(0)
b_auto_delete(false)
timeout(_timeout)
{
hStopEvent=WSACreateEvent();
hEvent=WSACreateEvent();

readO.Offset=0;
readO.OffsetHigh=0;
readO.hEvent=CreateEvent(NULL TRUE FALSE NULL);

writeO.Offset=0;
writeO.OffsetHigh=0;
writeO.hEvent=CreateEvent(NULL TRUE FALSE NULL);
}

async_socket::~async_socket()
{
WSASetEvent(hStopEvent);

socket_thread.wait_for_end();

if(the_socket!=INVALID_SOCKET){
shutdown();
::closesocket(the_socket);
the_socket=INVALID_SOCKET;
}

if(hEvent!=WSA_INVALID_EVENT){
WSACloseEvent(hEvent);
hEvent=WSA_INVALID_EVENT;
}

if(hStopEvent!=WSA_INVALID_EVENT){
WSACloseEvent(hStopEvent);
hStopEvent=WSA_INVALID_EVENT;
}
if(readO.hEvent!=NULL)
CloseHandle(readO.hEvent);
if(writeO.hEvent!=NULL)
CloseHandle(writeO.hEvent);
}

bool 
async_socket::bind(const ip_addr& addr)
{
::bind(the_socket &addr sizeof(ip_addr));
return true;
}

void 
async_socket::close()
{
WSAResetEvent(hEvent);
WSASetEvent(hStopEvent);
}

bool 
async_socket::init()
{
if(the_socket==INVALID_SOCKET)
return false;


WSAResetEvent(hStopEvent);

return socket_thread.create(static_thread_func this);
}

bool 
async_socket::create(
int nSocketType/* = SOCK_STREAM*/
long lEvent/* = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE*/
const ip_addr& addr/* =  ip_addr(00000)*/)
{
bool ret_val(true);

the_socket=socket( AF_INET nSocketType 0);

if(the_socket!=INVALID_SOCKET){
if(select(lEvent)==true){
if(bind(addr)==true)
if(init()==true)
return true;
}
}

close();

return false;
}

bool 
async_socket::select(long lEvent/* = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE */)
{
if(WSAEventSelect(  
the_socket
hEvent
lEvent)==SOCKET_ERROR){
return false;
}else{
l_events=lEvent;
return true;
}
}

int 
async_socket::static_thread_func(void* p)
{
return reinterpret_cast(p)->socket_func();
}

int 
async_socket::socket_func()
{
WSAEVENT handles[]={
hStopEvent
readO.hEvent
writeO.hEvent
hEvent};
WSANETWORKEVENTS netEvents;
DWORD wait_res;
DWORD dwTimeOUT;

on_attach();

for(;;){

if(timeout == 0)
dwTimeOUT=WSA_INFINITE;
else
dwTimeOUT=timeout;

wait_

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        2989  2000-11-27 20:43  HTTP_Server\main.cpp
     文件        1137  2000-11-27 20:43  HTTP_Server\prog_opt.cpp
     文件         710  2000-11-27 20:46  HTTP_Server\ip_addr.hpp
     文件         963  2000-11-27 20:47  HTTP_Server\thread.hpp
     文件        1299  2000-11-27 20:47  HTTP_Server\sinchro.hpp
     文件        1486  2000-11-27 20:47  HTTP_Server\prog_opt.hpp
     文件        2740  2000-11-27 20:46  HTTP_Server\respond.h
     文件        2126  2000-11-27 20:44  HTTP_Server\thread.cpp
     文件       24750  2000-11-27 20:44  HTTP_Server\respond.cpp
     文件        1080  2000-11-27 20:45  HTTP_Server\request.h
     文件        1548  2000-11-27 20:43  HTTP_Server\ip_addr.cpp
     文件        6776  2000-11-27 16:26  HTTP_Server\HTTP_Server.dsp
     文件        2118  2000-11-27 20:43  HTTP_Server\http_date.cpp
     文件        1547  2000-11-27 20:46  HTTP_Server\http_date.hpp
     文件      143360  2000-11-27 20:31  HTTP_Server\HTTP_Server.exe
     文件        2214  2000-11-27 20:46  HTTP_Server\async_socket.hpp
     文件         553  2000-11-27 20:46  HTTP_Server\global_defs.hpp
     文件        4997  2000-11-27 20:43  HTTP_Server\listen_socket.cpp
     文件        1574  2000-11-27 20:45  HTTP_Server\request_socket.h
     文件         852  2000-11-27 20:43  HTTP_Server\global_defs.cpp
     文件         448  2000-11-27 20:46  HTTP_Server\http_server.hpp
     文件        3584  2000-11-27 20:44  HTTP_Server\request_socket.cpp
     文件        1792  2000-11-27 20:44  HTTP_Server\listen_socket.h
     文件        4876  2000-11-27 20:44  HTTP_Server\request.cpp
     文件        8012  2000-11-27 20:43  HTTP_Server\async_socket.cpp
     文件         133  2000-11-27 20:43  HTTP_Server\http_server.cpp
     文件         545  2000-11-27 20:38  HTTP_Server\HTTP_Server.dsw
     文件       12446  2000-11-27 20:40  HTTP_Server\HTTP_Server.mak
     文件        2347  2000-11-27 20:40  HTTP_Server\HTTP_Server.dep

评论

共有 条评论