It's right to user CWinThread as mine
From: leehom (mixtrue_at_hotmail.com)
Date: 06/03/04
- Next message: Jon Evans: "Re: SubclassWindow() - Compiler bug ?"
- Previous message: Gary Chang: "Re: setting 'Use system font'"
- Next in thread: Ian Semmel: "Re: It's right to user CWinThread as mine"
- Reply: Ian Semmel: "Re: It's right to user CWinThread as mine"
- Reply: Joseph M. Newcomer: "Re: It's right to user CWinThread as mine"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 3 Jun 2004 15:12:31 -0700
Hi everyone , I write a thread class derived from CWinThread. When i create
a thread to run , the programm will be stop . Why? My code as following:
head File:
class CListeningThread : public CWinThread
{
DECLARE_DYNCREATE(CListeningThread)
//protected:
public:
CListeningThread(); // protected constructor used by dynamic
creation
CListeningThread(LPSTR address, int port);
virtual ~CListeningThread();
// Attributes
public:
WSADATA m_wsaData;
SOCKET m_Listen_skt,m_Accept_skt ;
sockaddr_in m_ServerInf;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CListeningThread)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual int Run ();
virtual BOOL SetSvrInf (LPSTR address, int port);
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CListeningThread)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CPP File:
IMPLEMENT_DYNCREATE(CListeningThread, CWinThread)
CListeningThread::CListeningThread()
{
}
CListeningThread::CListeningThread(LPSTR address, int port)
{
m_ServerInf.sin_addr.s_addr =inet_addr (address);
m_ServerInf.sin_family =AF_INET;
m_ServerInf.sin_port = htons(port);
}
CListeningThread::~CListeningThread()
{
}
BOOL CListeningThread::InitInstance()
{
// TODO: perform and per-thread initialization here
int Result = WSAStartup(MAKEWORD(2,2),&m_wsaData);
if (Result!=NO_ERROR){
return false;
}
return TRUE;
}
BOOL CListeningThread::SetSvrInf (LPSTR address, int port)
{
m_ServerInf.sin_addr.s_addr =inet_addr (address);
m_ServerInf.sin_family =AF_INET;
m_ServerInf.sin_port = htons(port);
return true;
}
int CListeningThread::ExitInstance()
{
WSACleanup ();
return CWinThread::ExitInstance();
}
int CListeningThread::Run ()
{ //DO YOUSELF THINGS
m_Listen_skt = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (m_Listen_skt==INVALID_SOCKET) {
WSACleanup ();
return 0;
}
if ( bind( m_Listen_skt, (SOCKADDR*) &m_ServerInf, sizeof(m_ServerInf) ) ==
SOCKET_ERROR ) {
closesocket(m_Listen_skt);
return 0;
}
if ( listen( m_Listen_skt, 1 ) == SOCKET_ERROR ) {
return 0;
}
while (1) {
m_Accept_skt = SOCKET_ERROR;
while ( m_Accept_skt == SOCKET_ERROR ) {
m_Accept_skt = accept( m_Listen_skt, NULL, NULL );
CRevThread* pThread = new CRevThread;
pThread->SetRecvHandle (m_Accept_skt);
pThread->CreateThread ();
}
//m_Listen_skt = m_Acceptskt;
break;
}
return 1;
//return CWinThread::Run ();
}
Then I use my class in main programm.
?????????????????????????????????????????????????????????
CListeningThread* pHandle = new CListeningThread;
pHandle->SetSvrInf ("192.168.0.234", 8999);
pHandle->CreateThread ();
?????????????????????????????????????????????????????????
After Run
pHandle->CreateThread ();
the programm is dead . Why ?
Thanks in advance.
Leehom
- Next message: Jon Evans: "Re: SubclassWindow() - Compiler bug ?"
- Previous message: Gary Chang: "Re: setting 'Use system font'"
- Next in thread: Ian Semmel: "Re: It's right to user CWinThread as mine"
- Reply: Ian Semmel: "Re: It's right to user CWinThread as mine"
- Reply: Joseph M. Newcomer: "Re: It's right to user CWinThread as mine"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|