📄 tcpcustom_ce.cpp
字号:
// TCPCustom_CE.cpp: implementation of the CTCPCustom_CE class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SendDataServer.h"
#include "TCPCustom_CE.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTCPCustom_CE::CTCPCustom_CE()
{
m_exitThreadEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
}
CTCPCustom_CE::~CTCPCustom_CE()
{
CloseHandle(m_exitThreadEvent);
}
DWORD CTCPCustom_CE::SocketThreadFunc(PVOID lparam)
{
CTCPCustom_CE *pSocket;
//得到CTCPCustom 类实例指针
pSocket=(CTCPCustom_CE*)lparam;
//定义读事件集合
fd_set fdRead;
int ret;
TIMEVAL aTime;
aTime.tv_sec=1;
aTime.tv_usec=0;
while (TRUE)
{
//收到退出事件,结束线程
if(WaitForSingleObject(pSocket->m_exitThreadEvent,0) ==WAIT_OBJECT_0)
{
break;
}
// 置空读事件集合
FD_ZERO(&fdRead);
//给 pSocket设置读事件
FD_SET(pSocket->m_socket,&fdRead);
//调用select函数,判断是否有读事件发生
ret=select(0,&fdRead,NULL,NULL,&aTime);
if(ret==SOCKET_ERROR)
{
//触发错误事件
pSocket->m_pTCPServer_CE->OnClientError(pSocket->m_pTCPServer_CE->m_pOwnerWnd,pSocket,1);
//关闭socket
closesocket(pSocket->m_socket);
break;
}
if(ret>0)
{
// 判断是否读事件
if(FD_ISSET(pSocket->m_socket,&fdRead))
{
char recvBuf[1024];
int recvLen;
ZeroMemory(recvBuf,1024);
recvLen=recv(pSocket->m_socket,recvBuf,1024,0);
if(recvLen==SOCKET_ERROR)
{
int nErrorCode=WSAGetLastError();
//触发与客户端连接的socket 错误
pSocket->m_pTCPServer_CE->OnClientError(pSocket->m_pTCPServer_CE->m_pOwnerWnd,pSocket,nErrorCode);
//触发与客户端连接的socket关闭事件
pSocket->m_pTCPServer_CE->OnClientClose(pSocket->m_pTCPServer_CE->m_pOwnerWnd,pSocket);
//关闭事件
closesocket(pSocket->m_socket);
break;
}
//表示连接已经从容关闭
else if(recvLen==0)
{
pSocket->m_pTCPServer_CE->OnClientClose(pSocket->m_pTCPServer_CE->m_pOwnerWnd,pSocket);
//关闭socket
closesocket(pSocket->m_socket);
break;
}
else
{
//触发与客户端连接的Socket读事件
pSocket->m_pTCPServer_CE->OnClientRead(pSocket->m_pTCPServer_CE->m_pOwnerWnd,pSocket,recvBuf,recvLen);
}
}
}
}
return 0;
}
bool CTCPCustom_CE::Open(CTCPServer_CE *pTCPServer)
{
//创建通讯线程
m_tcpThreadHandle=CreateThread(NULL,0,SocketThreadFunc,this,0,NULL);
if(m_tcpThreadHandle==NULL)
{
closesocket(m_socket);
return FALSE;
}
//设置通讯模式为异步模式
DWORD ul=1;
ioctlsocket(m_socket,FIONBIO,&ul);
m_pTCPServer_CE=pTCPServer;
return TRUE;
}
bool CTCPCustom_CE::Close()
{
//发送通讯线程结束事件
SetEvent(m_exitThreadEvent);
Sleep(1000);
//关闭socket,释放资源
int err=closesocket(m_socket);
if(err==SOCKET_ERROR)
{
return FALSE;
}
return TRUE;
}
bool CTCPCustom_CE::SendData(const char *buf, int len)
{
int nBytes=0;
int nSendBytes=0;
while (nSendBytes<len)
{
nBytes=send(m_socket,buf+nSendBytes,len-nSendBytes,0);
if(nBytes==SOCKET_ERROR)
{
int iErrorCode=WSAGetLastError();
//触发socket的Error事件
m_pTCPServer_CE->OnClientError(m_pTCPServer_CE->m_pOwnerWnd,this,iErrorCode);
//触发与服务器端断开连接事件
m_pTCPServer_CE->OnClientClose(m_pTCPServer_CE->m_pOwnerWnd,this);
//关闭socket
Close();
return FALSE;
}
nSendBytes=nSendBytes +nBytes;
if(nSendBytes<len)
{
Sleep(1000);
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -