Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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 />


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 />


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:
<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 not working... Anyone knows why ?
Thank you...
Posted
Updated 7-Apr-10 10:44am
v2

1 solution

I recomment not using CSocket because it is unreliable. It also fakes blocking networking by running a small message pump every time it gets a WSAEWOULDBLOCK error, it is easier to just use normal Winsock. As an alternative, there are many C++ network classes out there, in alphabetical order:

* Alhem
* Boost
* SharkEngine
* QT
* and many others...

Hope this helps!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900