⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysocket.cpp

📁 井字棋
💻 CPP
字号:
// MySocket.cpp : implementation file
//

#include "stdafx.h"
#include "JingZiQi.h"
#include "MySocket.h"
#include "MainFrm.h"
#include "JingZiQiView.h"

//#define TCOLOR RGB(100,255,0)
//#define BCOLOR RGB(100,0,155)

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMySocket

CMySocket::CMySocket()
{
}

CMySocket::~CMySocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CMySocket, CSocket)
	//{{AFX_MSG_MAP(CMySocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CMySocket member functions

void CMySocket::OnAccept(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	CMySocket *pSocket=new CMySocket;
	CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	if(pFrame->m_pListen->Accept(*pSocket))
	{
		pFrame->m_List.AddTail(pSocket);
		pFrame->m_pConnect=pSocket;
		MessageBox(pFrame->m_hWnd,"接受了一个请求!","接受请求",MB_OK);
	}
	else
		delete pSocket;
//	pSocket->ShutDown();
	CSocket::OnAccept(nErrorCode);
}

void CMySocket::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	CJingZiQiView *pView=(CJingZiQiView *)(pFrame->GetActiveView());

	char pMsg[10000],tmpMsg[1000];
	int ByteCount,End=0;
	CStringArray *temp=new CStringArray;

	strcpy(pMsg,"");
	do{
		strcpy(tmpMsg,"");
		ByteCount=this->Receive(tmpMsg,1000);
		if(ByteCount>1000||ByteCount<=0)
		{
			MessageBox(pFrame->m_hWnd,"接受网络信息发生错误!","错误",MB_OK);
			return ;
        }
		else if(ByteCount<1000&&ByteCount>0)
		{
			End=1;
		}
		tmpMsg[ByteCount]=0;
		strcat(pMsg,tmpMsg);
	}while(!End);
	
	char tmp1[8],tmp2[2];
	strcpy(tmp1,"");
	strcpy(tmp2,"");
	strncat(tmp1,pMsg,4);
	strncat(tmp2,pMsg,2);
	int p=strcmp(tmp2,"P:");
	int q=strcmp(tmp2,"Q:");
	if(strcmp(tmp1,"MSG:")==0)
	{
		MessageBox(AfxGetApp()->m_pMainWnd->m_hWnd,pMsg,"提醒你",MB_OK);
		pView->Empty();
		pView->over=0;
	/*	this->ShutDown();
		//this->Close();
		delete this;
		if(pView->IsServer)
		{
			pFrame->m_pConnect=NULL;
			delete pFrame->m_pListen;
			pFrame->m_pListen=NULL;
		}*/
		if(!pView->IsServer)
		{
			delete pFrame->m_pClient;
			pFrame->m_pClient=NULL;
		}

		return;
	}
   if(p==0||q==0)
	{
	
	   pView->ShowReq_Rep(pMsg);
		if(q==0)
			pView->requ_reply=false;
		return;
	}
		char sx[3];
		char sy[6];
		strcpy(sx,"");
		strncat(sx,pMsg,2);
		strcpy(sy,"");
		for(int i=0;i<5;i++)
			sy[i]=pMsg[i+2];
		
		pFrame->m_point.x=atoi(sx);
		pFrame->m_point.y=atoi(sy);
	
		pView->OpponentDone();
	CSocket::OnReceive(nErrorCode);
}

void CMySocket::OnClose(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	Close();
	CSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -