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

📄 clientsocket.cpp

📁 双向CS通信程序!是一个模拟网络通信的很好的实例程序!是用VC编的。适用window的操作系统。
💻 CPP
字号:
// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ClientSocket.h"
#include "MainFrm.h"//包含上框架类的头文件

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

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

//构造函数和析构函数的实现

CClientSocket::CClientSocket(CMainFrame *pMainFrame)
{
	m_pMainFrame=pMainFrame;
}

CClientSocket::~CClientSocket()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions


void CClientSocket::OnReceive(int nErrorCode)
{
	char pMsg[10000],tempMsg[1000];//用来接收数据而用的变量
	int ByteCount;//每次读取的字符个数
	int EndFlag=0; //接收完毕的标志


	//初始化接收的数据

	strcpy(pMsg,"");
	do
	{
		//每次读取时,把读取使用的缓冲区置成空
		strcpy(tempMsg,"");
        //每次读取1000个字符
		ByteCount=Receive(tempMsg,1000);

		//以下是一种读取数据时可能产生的错误情况

		if(ByteCount>1000||ByteCount<=0)
		{
			AfxMessageBox("接收数据中出错",MB_OK);
			return;
		}
		else if(ByteCount<1000&&ByteCount>0)
		{
			//如果读取的字节数不足1000,则说明数据已经读取完毕
			EndFlag=1;
		}

		//保证结束位被加上
		tempMsg[ByteCount]=0;
		strcat(pMsg,tempMsg);
	}
	while(EndFlag==0);

	AfxMessageBox(pMsg,MB_OK);//显示接收的信息

	CSocket::OnReceive(nErrorCode);
}

⌨️ 快捷键说明

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