本文介绍了CSocket&的问题OnReceive()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在尝试将tcp服务器/客户端通信模块写入我的软件.我的CNewClientSocket存在问题-没有对OnReceive()函数的输入.
ServerThread:

Hello
I''m trying to write tcp server/client communication module to my software. I have problem with my CNewClientSocket - no entry to OnReceive() function.
ServerThread:

<br />
	if ( serverSocket.Create( SERVER_PORT ) ) {<br />
		while ( working ) {<br />
			if ( serverSocket.Listen() ) {<br />
				if ( serverSocket.Accept( clientSocket, (SOCKADDR*)&clientAddress, &addressSize  ) ) {<br />
					CClientThread* newClientThread = (CClientThread*)AfxBeginThread( RUNTIME_CLASS(CClientThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );<br />
					newClientThread->setSocketHandle(clientSocket.m_hSocket);<br />
					newClientThread->setSocketAddress( clientAddress );<br />
					newClientThread->setParent(this);<br />
					clientsMap.SetAt( newClientThread, clientAddress );<br />
					clientSocket.Detach();<br />
					newClientThread->ResumeThread();		<br />
				}<br />
			} else {<br />
					// ERROR}<br />
		}<br />
	} else {<br />
		//ERROR<br />
	}<br />



ClientThread上的InitInstance:



InitInstance on ClientThread:

<br />
	if (!AfxSocketInit()) {<br />
		return FALSE;<br />
	}<br />
<br />
	socket.Attach( handle );<br />
	socket.Send( "HELLO", 5 );<br />
<br />
	char bufor[4096];<br />
	int bytes = 0;<br />
<br />
	while ( TRUE ) {<br />
		::Sleep(100);<br />
	}<br />
	AfxEndThread(0);<br />
	return TRUE;<br />



我的套接字-是CNewClientSocket的实例,该实例派生自CSocket:

NewClientSocket.h:



My socket - is instance of CNewClientSocket which is derived from CSocket:

NewClientSocket.h:

<br />
#pragma once<br />
<br />
// CNewClientSocket command target<br />
<br />
class CNewClientSocket : public CSocket<br />
{<br />
public:<br />
//	DECLARE_DYNAMIC( CNewClientSocket )<br />
	CNewClientSocket();<br />
	virtual ~CNewClientSocket();<br />
<br />
	//{{AFX_VIRTUAL(CNewClientSocket) <br />
public:<br />
	virtual void OnReceive(int nErrorCode); <br />
	virtual void OnClose(int nErrorCode);<br />
	//}}AFX_VIRTUAL <br />
<br />
	// Generated message map functions<br />
	//{{AFX_MSG(CNewClientSocket)<br />
	// NOTE - the ClassWizard will add and remove member functions here.<br />
	//}}AFX_MSG<br />
};<br />
<br />



NewClientSocket.cpp:



NewClientSocket.cpp:

<br />
// NewClientSocket.cpp : implementation file<br />
//<br />
<br />
#include "stdafx.h"<br />
#include "DASim.h"<br />
#include "NewClientSocket.h"<br />
<br />
<br />
// CNewClientSocket<br />
<br />
<br />
//IMPLEMENT_DYNAMIC( CNewClientSocket, CSocket )<br />
<br />
CNewClientSocket::CNewClientSocket() {<br />
}<br />
<br />
CNewClientSocket::~CNewClientSocket() {<br />
}<br />
<br />
#if 0<br />
BEGIN_MESSAGE_MAP(CNewClientSocket, CSocket) <br />
	//{{AFX_MSG_MAP(CNewClientSocket) <br />
	//}}AFX_MSG_MAP <br />
END_MESSAGE_MAP() <br />
#endif<br />
<br />
// CNewClientSocket member functions<br />
<br />
void CNewClientSocket::OnReceive(int nErrorCode) {<br />
	CAsyncSocket::OnReceive(nErrorCode);<br />
}<br />
<br />
void CNewClientSocket::OnClose(int nErrorCode)<br />
{<br />
	// TODO: Add your specialized code here and/or call the base class<br />
<br />
	CAsyncSocket::OnClose(nErrorCode);<br />
}<br />



OnReceive无法正常工作...有人知道为什么吗?
谢谢...



OnReceive not working... Anyone knows why ?
Thank you...

推荐答案


这篇关于CSocket&amp;的问题OnReceive()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:39