acceptsocket.cpp

来自「双向CS通信程序!是一个模拟网络通信的很好的实例程序!是用VC编的。适用wind」· C++ 代码 · 共 79 行

CPP
79
字号
// AcceptSocket.cpp : implementation file
//

#include "stdafx.h"
#include "server.h"
#include "AcceptSocket.h"

#include"MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAcceptSocket

CAcceptSocket::CAcceptSocket(CMainFrame *pMainFrame)
{
	//给框架对象进行赋值
	m_pMainFrame=pMainFrame;
}

CAcceptSocket::~CAcceptSocket()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CAcceptSocket member functions

void CAcceptSocket::OnReceive(int nErrorCode)
{
	char pMsg[10000],tempMsg[1000];//用来进行数据接收的变量
	int ByteCount;//在接收数据时,用来计数的变量
	int EndFlag=0;
	char AnswerMsg[10100];//回应信息字符串

	//把数据字符串赋值为空

	strcpy(pMsg,"");
	do
	{
		//把每次接收的字符串清空
		strcpy(tempMsg,"");
		ByteCount=Receive(tempMsg,1000);
		if(ByteCount>1000||ByteCount<=0)
		{
			AfxMessageBox("接收数据出错",MB_OK);
			return;
		}
		else if(ByteCount<1000&&ByteCount>0)
		{
			EndFlag=1;
		}
		tempMsg[ByteCount]=0;
		//把每次接收到的数据放在一起
		strcat(pMsg,tempMsg);

	}
	while(EndFlag==0);
	AfxMessageBox(pMsg,MB_OK);
    
	sprintf(AnswerMsg,"收到客户信息\n%s\n谢谢",pMsg);
	//AfxMessageBox(AnswerMsg,MB_OK);

	Send(AnswerMsg,strlen(AnswerMsg),0);
	CSocket::OnReceive(nErrorCode);
}

⌨️ 快捷键说明

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