socket.cpp

来自「Client 和Server 配套的网络连接程序。」· C++ 代码 · 共 61 行

CPP
61
字号
// Socket.cpp : implementation file
//

#include "stdafx.h"
#include "ClientProgram.h"
#include "Socket.h"

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

/////////////////////////////////////////////////////////////////////////////
// Socket

Socket::Socket()
{
}

Socket::~Socket()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// Socket member functions

void Socket::OnReceive(int nErrorCode) 
{
	long dataLength;
	Receive(&dataLength, 4);
	
	if (dataLength > 0 && dataLength < 100000) //Just to make sure the server doesn't crash the client
	{
		byte* message = new byte[dataLength+1];

		int nRec = Receive(message,dataLength);
		message[nRec] = '\0';

		::SendMessage(hParent,WM_RECEIVE_MESSAGE,(WPARAM)message,(LPARAM)nSocket);
	}

	CSocket::OnReceive(nErrorCode);
}

void Socket::OnClose(int nErrorCode) 
{
	::SendMessage(hParent,WM_DISCONNECT,NULL,NULL);
	CSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

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