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

📄 serverthread.cpp

📁 自制的双子球游戏
💻 CPP
字号:
// ServerThread.cpp : implementation file
//

#include "stdafx.h"
#include "DBallGame.h"
#include "ServerThread.h"

#include "MyDef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNetThread

IMPLEMENT_DYNCREATE(CServerThread, CWinThread)

CServerThread::CServerThread()
{
}

CServerThread::~CServerThread()
{
}

BOOL CServerThread::InitInstance()
{
	m_bReceiving = 0;
	ret = 0;
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CServerThread::ExitInstance()
{
	closesocket(s);
	closesocket(m_ListenSocket);
	m_client->ExitInstance ();
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CServerThread, CWinThread)
	//{{AFX_MSG_MAP(CServerThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetThread message handlers
void CServerThread::Exchange (CDBallGameDlg* p)
{
	p_CDBallGameDlg = p;
}




UINT CServerThread::ListeningThrd(void * pParam)
{
	    CServerThread * pSock = (CServerThread *) pParam;
		
		if (pSock != NULL)
		{
			pSock->Listening ();
			pSock->ReceivingLoop ();
		}
		return 1;
		
}

void CServerThread::StartListen()
{
	    	
	m_client = AfxBeginThread((AFX_THREADPROC)CServerThread::ListeningThrd, this);
//	ListeningThrd();
}

void CServerThread::ReceivingLoop()
{
	CString str;
	char buff[1];
	while(m_bReceiving)
	{
		ret = recv(s,buff,1,0);
		if(ret == 0||ret == SOCKET_ERROR)
		{
			//AfxMessageBox("recv出错");
			continue;
		}
		switch(buff[0])
		{
		case GAME_BEGIN:
			{
				p_CDBallGameDlg->BeginGame ();
				break;
			}
		case GAME_END:
			{
				p_CDBallGameDlg->Reset ();
				break;
			}
		case GO_LEFT:
			{
				p_CDBallGameDlg->Bar_Above.GoLeft ();
				p_CDBallGameDlg->p_Ball->SetBarPos (p_CDBallGameDlg->Bar_Above.GetPos (),BAR_ABOVE);
				break;
			}
		case GO_RIGHT:
			{
				p_CDBallGameDlg->Bar_Above .GoRight ();
				p_CDBallGameDlg->p_Ball->SetBarPos (p_CDBallGameDlg->Bar_Above.GetPos (),BAR_ABOVE);
				break;
			}
		}
		//str.Format ("%c",buff[0]);
		//AfxMessageBox(str);
	}

}

void CServerThread::Listening()
{
	m_ListenSocket = socket(AF_INET,SOCK_STREAM,0);
	CString str;
	//str.Format("210.30.52.207");
	//long int ip;
	//ip=inet_addr(str);
	addr.sin_addr.S_un .S_addr = INADDR_ANY;
	addr.sin_port = htons(10025);
	addr.sin_family = AF_INET;
	ret = bind(m_ListenSocket,(LPSOCKADDR)&addr,sizeof(addr));
	if(ret == SOCKET_ERROR)
	{
		AfxMessageBox("bind");
		return;
	}
	ret = listen (m_ListenSocket,1);
	if(ret==SOCKET_ERROR)
	{
		AfxMessageBox("listen失败");
		return ;
	}
	s = accept(m_ListenSocket,NULL,NULL);
	if(s == SOCKET_ERROR)
	{
		AfxMessageBox("accept失败");
		return ;
	}
	m_bReceiving = 1;

}


void CServerThread::GoLeft()
{
	char *buf= "L";
	
	ret  = send(s,buf,1,0);
}

void CServerThread::GoRight()
{	
	char *buf= "R";
	
	ret  = send(s,buf,1,0);

}

void CServerThread::Start()
{
	char *buf= "B";
	
	ret  = send(s,buf,1,0);

}

⌨️ 快捷键说明

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