Re: Using CWinThread in MFC apps
Thanks guys for the help..
I ran into another problem now.
My CSocket::OnReceive is never called.
I searched the net and some website suggested
that I was stucked in a loop instead of the message pump
I looked here and there for a couple of hours but still
couldn't find the bug...
Could you please help?
[code]
// Socket.cpp : implementation file
//
#include "stdafx.h"
#include "CDemo.h"
#include "Socket.h"
#include ".\socket.h"
class CCDemoDoc;
UINT __cdecl Sock_Handler(LPVOID arg);
SOCKET hConnected;
// cSocket
cSocket::cSocket()
{
OutputDebugString("cSocket::cSocket\n");
}
cSocket::cSocket(CCDemoDoc *pDoc)
{
m_Doc = pDoc;
}
cSocket::~cSocket()
{
OutputDebugString("cSocket::~cSocket\n");
}
// cSocket member functions
void cSocket::OnAccept(int nErrorCode)
{
OutputDebugString("cSocket::OnAccept\n");
//Accept(this);
Attach(hConnected);
// TODO: Add your specialized code here and/or call the base class
AfxBeginThread(Sock_Handler, this);
CSocket::OnAccept(nErrorCode);
}
void cSocket::OnClose(int nErrorCode)
{
OutputDebugString("cSocket::OnClose\n");
// TODO: Add your specialized code here and/or call the base class
CSocket::OnClose(nErrorCode);
}
void cSocket::OnConnect(int nErrorCode)
{
OutputDebugString("cSocket::OnConnect\n");
// TODO: Add your specialized code here and/or call the base class
CSocket::OnConnect(nErrorCode);
}
void cSocket::OnOutOfBandData(int nErrorCode)
{
OutputDebugString("cSocket::OnOutOfBandData\n");
// TODO: Add your specialized code here and/or call the base class
CSocket::OnOutOfBandData(nErrorCode);
}
void cSocket::OnReceive(int nErrorCode)
{
OutputDebugString("cSocket::OnReceive\n");
char buff[4096];
int nRead;
nRead = Receive(buff, 4096);
if (nRead != SOCKET_ERROR)
{
if (nRead)
{
buff[nRead] = 0;
CString szTemp(buff);
// ((CMsocsDlg*)m_pDlg)->m_strRecv += szTemp;
// ((CMsocsDlg*)m_pDlg)->UpdateData(FALSE);
// if (szTemp.CompareNoCase("bye") == 0 ) ShutDown();
}
else Close();
}
else
{
wsprintf (buff, "Error number is %d", GetLastError());
AfxMessageBox (buff);
}
CSocket::OnReceive(nErrorCode);
// TODO: Add your specialized code here and/or call the base class
}
void cSocket::OnSend(int nErrorCode)
{
OutputDebugString("cSocket::OnSend\n");
// TODO: Add your specialized code here and/or call the base class
CSocket::OnSend(nErrorCode);
}
int cSocket::Receive(void* lpBuf, int nBufLen, int nFlags)
{
OutputDebugString("cSocket::Receive\n");
// TODO: Add your specialized code here and/or call the base class
return CSocket::Receive(lpBuf, nBufLen, nFlags);
}
int cSocket::Send(const void* lpBuf, int nBufLen, int nFlags)
{
OutputDebugString("cSocket::Send\n");
// TODO: Add your specialized code here and/or call the base class
return CSocket::Send(lpBuf, nBufLen, nFlags);
}
BOOL cSocket::OnMessagePending()
{
OutputDebugString("cSocket::OnMessagePending\n");
// TODO: Add your specialized code here and/or call the base class
return CSocket::OnMessagePending();
}
//////////////////////////////////////
UINT __cdecl Sock_Handler(LPVOID arg)
{
cSocket *pSock = (cSocket*) arg;
while(true)
{
if (!pSock->buff.empty())
{
// pSock->CopyDataToPacket();
pSock->buff.clear();
}
Sleep(500);
}
return 0;
}
[/code]