📄 clientsock.cpp
字号:
// ClientSock.cpp : implementation file
//
#include "stdafx.h"
#include "chinesechess.h"
#include "ClientSock.h"
#include "ChineseChessDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSock
CClientSock::CClientSock()
{
m_aSessionIn=NULL;
m_aSessionOut=NULL;
m_sfSocketFile=NULL;
m_bInit=false;
m_bClose=false;
}
CClientSock::~CClientSock()
{
if(m_aSessionIn)
delete m_aSessionIn;
if(m_aSessionOut)
delete m_aSessionOut;
if(m_sfSocketFile)
delete m_sfSocketFile;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSock, CSocket)
//{{AFX_MSG_MAP(CClientSock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CClientSock member functions
void CClientSock::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CSocket::OnReceive(nErrorCode);
do
{
CMyMessage temp;
CChineseChessApp* pApp=(CChineseChessApp*)AfxGetApp();
CChineseChessDlg* pDlg=(CChineseChessDlg*)pApp->m_pMainWnd;
temp.Serialize(*m_aSessionIn);
// CString str;
// str.Format("id:%d fx%d:fy:%d tx:%dty:%d turn%d",temp.m_ChessID,temp.m_FromX,
// temp.m_FromY,temp.m_ToX,temp.m_ToY,temp.m_turn);
// AfxMessageBox(str);
if(temp.m_strText.GetLength()>1)
{
pDlg->m_BlackName.SetWindowText(temp.m_hostname);
pDlg->m_MsgList.AddString(temp.m_hostname+"说"+temp.m_strText);
pDlg->m_EditSendMsg.SetWindowText(_T(""));;
}
if(temp.iFlag == 0)
{
pDlg->m_byShowChessBoard[9-temp.m_FromX][8-temp.m_FromY]=NoChessMan;
pDlg->m_byShowChessBoard[9-temp.m_ToX][8-temp.m_ToY]=
temp.m_ChessID;
pDlg->m_iWhoChess=temp.m_turn;
memcpy(pDlg->m_byChessBoard,pDlg->m_byShowChessBoard,90);
memcpy(pDlg->m_byBackupChessBoard,pDlg->m_byShowChessBoard,90);
CRect rect(BoarderWidth,BoarderHeight,BoarderWidth+9*GridWidth,10*BoarderHeight);
pDlg->InvalidateRect(&rect,TRUE);//刷新窗口
pDlg->UpdateWindow();
pDlg->InvalidateRect(NULL,FALSE);
pDlg->UpdateWindow();
if(pDlg->IsGameOver(pDlg->m_byChessBoard))
AfxMessageBox("你已输掉了,请重新开始");
}
}
while (!m_aSessionIn->IsBufferEmpty());
}
void CClientSock::Init()
{
m_sfSocketFile= new CSocketFile(this);
m_aSessionIn=new CArchive(m_sfSocketFile,CArchive::load);
m_aSessionOut=new CArchive(m_sfSocketFile,CArchive::store);
m_bClose=false;
m_bInit=true;
}
BOOL CClientSock::SendMessage(CMyMessage * msg)
{
if (m_aSessionOut != NULL)
{
msg->Serialize(*m_aSessionOut);
m_aSessionOut->Flush();
return TRUE;
}
else
{
//对方关闭了连接
m_bClose=true;
CloseSocket();
return FALSE;
}
}
void CClientSock::CloseSocket()
{
if(m_aSessionIn)
{
delete m_aSessionIn;
m_aSessionIn=NULL;
}
if(m_aSessionOut)
{
delete m_aSessionOut;
m_aSessionOut=NULL;
}
if(m_sfSocketFile)
{
delete m_aSessionOut;
m_sfSocketFile=NULL;
}
AfxMessageBox("对方断开连接!");
Close();
m_bInit=false;
m_bClose=true;
}
void CClientSock::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_bClose=true;
CloseSocket();
// m_view->CloseSessionSocket();
CSocket::OnClose(nErrorCode);
}
int CClientSock::GetLocalHostName(CString &sHostName) //获得本地计算机名称
{
char szHostName[256];
int nRetCode;
nRetCode=gethostname(szHostName,sizeof(szHostName));
if(nRetCode!=0)
{
//产生错误
sHostName=_T("没有取得");
return GetLastError();
}
sHostName=szHostName;
return 0;
}
int CClientSock::GetIpAddress(const CString &sHostName, CString &sIpAddress)//获得本地IP
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
//产生错误
sIpAddress=_T("");
return GetLastError();
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
sIpAddress=inet_ntoa(inAddr);
if(sIpAddress.IsEmpty())
sIpAddress=_T("没有取得");
}
return 0;
}
int CClientSock::GetIpAddress(const CString &sHostName, BYTE &f0,BYTE &f1,BYTE &f2,BYTE &f3)//获得本地IP
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
//产生错误
f0=f1=f2=f3=0;
return GetLastError();
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
f0=inAddr.S_un.S_un_b.s_b1;
f1=inAddr.S_un.S_un_b.s_b2;
f2=inAddr.S_un.S_un_b.s_b3;
f3=inAddr.S_un.S_un_b.s_b4;
}
return 0;
}
CString CClientSock::ErrorReason(int tag)
{
CString result;
switch(tag)
{
case WSANOTINITIALISED:
result="A successful AfxSocketInit must occur before using this API.";
break;
case WSAENETDOWN:
result="the network subsystem failed";
break;
case WSAEADDRINUSE:
result="The specified address is already in use";
break;
case WSAEINPROGRESS:
result="A blocking Windows Sockets call is in progress";
break;
case WSAEADDRNOTAVAIL:
result="The specified address is not available from the local machine";
break;
case WSAEAFNOSUPPORT:
result="Addresses in the specified family cannot be used with this socket";
break;
case WSAECONNREFUSED:
result="The attempt to connect was rejected";
break;
case WSAEDESTADDRREQ:
result="A destination address is required";
break;
case WSAEFAULT:
result="The nSockAddrLen argument is incorrect";
break;
case WSAEINVAL:
result="Invalid host address";
break;
case WSAEISCONN:
result="The socket is already connected.";
break;
case WSAEMFILE:
result="No more file descriptors are available";
break;
case WSAENETUNREACH:
result="The network cannot be reached from this host at this time";
break;
case WSAENOBUFS:
result="No buffer space is available. The socket cannot be connected";
break;
case WSAENOTSOCK:
result="The descriptor is not a socket";
break;
case WSAETIMEDOUT:
result="Attempt to connect timed out without establishing a connection";
break;
case WSAEWOULDBLOCK:
result="The socket is marked as nonblocking and the connection cannot be completed immediately.";
break;
default:
result="unknown error";
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -