📄 lansocket.cpp
字号:
// LanSocket.cpp : implementation file
//
#include "stdafx.h"
#include "LANServerClient.h"
#include "LanSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLanSocketServer
CLanSocketServer::CLanSocketServer()
{
m_pSocket = new CLanSocket;
}
CLanSocketServer::~CLanSocketServer()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CLanSocketServer, CAsyncSocket)
//{{AFX_MSG_MAP(CLanSocketServer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CLanSocketServer member functions
void CLanSocketServer::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
//侦听到连接请求,调用Accept函数
CLanSocket *pSocket = new CLanSocket;
if(Accept(*pSocket))
{
delete m_pSocket;
pSocket->AsyncSelect(FD_READ | FD_CLOSE);
m_pSocket = pSocket;
m_pSocket->SetMessagePara(m_pWnd,m_UserMsg);
m_pSocket->m_bAccept = TRUE;
m_pWnd->SendMessage(m_UserMsg, ACCEPT);
}
else
delete pSocket;
// CAsyncSocket::OnAccept(nErrorCode);
}
BOOL CLanSocketServer::SendStr(CString m_str)
{
return m_pSocket->SendStr(m_str);
}
CString CLanSocketServer::GetMessStr()
{
return m_pSocket->GetMessStr();
}
void CLanSocketServer::SetMessagePara(CWnd *pParent, UINT m_nMessageID)
{
m_UserMsg = m_nMessageID;
m_pWnd = pParent;
m_pSocket->SetMessagePara(m_pWnd,m_UserMsg);
}
void CLanSocketServer::CloseServer()
{
m_pSocket->Close();
delete m_pSocket;
Close();
}
/////////////////////////////////////////////////////////////////////////////
// CLanSocketClient
CLanSocketClient::CLanSocketClient()
{
m_bConnected = FALSE;
}
CLanSocketClient::~CLanSocketClient()
{
//关闭套接字
if(m_hSocket != INVALID_SOCKET)
Close();
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CLanSocketClient, CAsyncSocket)
//{{AFX_MSG_MAP(CLanSocketClient)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CLanSocketClient member functions
void CLanSocketClient::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if (nErrorCode == 0)
{
m_bConnected = TRUE;
m_pWnd->SendMessage(m_UserMsg, CONNECT);
// 提请一个“读”的网络事件,准备接收
AsyncSelect(FD_READ | FD_CLOSE);
}
//CAsyncSocket::OnConnect(nErrorCode);
}
void CLanSocketClient::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
int m_nLength = Receive(m_szBuffer, 4096);
m_szBuffer[m_nLength]='\0';
m_MessStr = m_szBuffer;
m_pWnd->SendMessage(m_UserMsg, CLIENTREAD);
//CAsyncSocket::OnReceive(nErrorCode);
}
void CLanSocketClient::OnSend(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
while(Send(m_szBuffer, strlen(m_szBuffer)) == SOCKET_ERROR);
m_MessStr = "Message Sended !";
m_pWnd->SendMessage(m_UserMsg, CLIENTSEND);
//继续提请一个“读”的网络事件,接收Server消息
AsyncSelect(FD_READ | FD_CLOSE);
//CAsyncSocket::OnSend(nErrorCode);
}
void CLanSocketClient::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_pWnd->SendMessage(m_UserMsg, SERVERCLOSE);
CAsyncSocket::OnClose(nErrorCode);
}
BOOL CLanSocketClient::IsConnect()
{
return m_bConnected;
}
BOOL CLanSocketClient::SendStr(CString m_str)
{
if(m_bConnected)
{
strcpy(m_szBuffer, LPCTSTR(m_str));
AsyncSelect(FD_WRITE);
return TRUE;
}
return FALSE;
}
CString CLanSocketClient::GetMessStr()
{
return m_MessStr;
}
void CLanSocketClient::Rest()
{
m_hSocket = INVALID_SOCKET;
m_bConnected = FALSE;
}
void CLanSocketClient::SetMessagePara(CWnd *pParent, UINT m_nMessageID)
{
m_UserMsg = m_nMessageID;
m_pWnd = pParent;
}
/////////////////////////////////////////////////////////////////////////////
// CLanSocket
CLanSocket::CLanSocket()
{
m_bAccept = FALSE;
}
CLanSocket::~CLanSocket()
{
if(m_hSocket != INVALID_SOCKET)
Close();
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CLanSocket, CAsyncSocket)
//{{AFX_MSG_MAP(CLanSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CLanSocket member functions
void CLanSocket::OnSend(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
while(Send(m_szBuffer, strlen(m_szBuffer)) == SOCKET_ERROR);
m_MessStr = "Message Sended !";
m_pWnd->SendMessage(m_UserMsg, SERVERSEND);
//继续提请一个“读”的网络事件,接收Server消息
AsyncSelect(FD_READ | FD_CLOSE);
// CAsyncSocket::OnSend(nErrorCode);
}
void CLanSocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_pWnd->SendMessage(m_UserMsg, CLIENTCLOSE);
CAsyncSocket::OnClose(nErrorCode);
}
void CLanSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
int m_nLength = Receive(m_szBuffer, 4096);
m_szBuffer[m_nLength]='\0';
m_MessStr = m_szBuffer;
m_pWnd->SendMessage(m_UserMsg, SERVERREAD);
// CAsyncSocket::OnReceive(nErrorCode);
}
BOOL CLanSocket::SendStr(CString m_str)
{
if(m_bAccept)
{
strcpy(m_szBuffer, LPCTSTR(m_str));
AsyncSelect(FD_WRITE);
return TRUE;
}
return FALSE;
}
CString CLanSocket::GetMessStr()
{
return m_MessStr;
}
void CLanSocket::SetMessagePara(CWnd *pParent, UINT m_nMessageID)
{
m_UserMsg = m_nMessageID;
m_pWnd = pParent;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -