It's right to user CWinThread as mine

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: leehom (mixtrue_at_hotmail.com)
Date: 06/03/04


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



Relevant Pages

  • Re: Wozu noch DOS?
    ... Das meiste davon wird genauso unter MS-DOS 4.0 funktionieren. ... ein Programm mit einer Library linken, wenn man die Library nutzen will. ... Programm sie verwenden will, ist eigentlich nur ein nettes Gimmick. ... Instruktion 'int 10' zu 0xA0000000 springt. ...
    (de.comp.os.msdos)
  • Re: Include files with function prototypes and variable declaration/definition
    ... I would like to write a C programm with multiple include files, ... multiple definition of 'value'. ... extern int value; ... That's a declaration, but not a definition, so it reserves no storage. ...
    (comp.lang.c)
  • Re: How do I do __typeof(__box MyValueType) in the new C++/CLI syn
    ... but perhaps we'd want to test a bit more before declaring the two instances of the value type equal... ... virtual bool Equals(System::Object^ obj) override ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Programms memory adress location access?
    ... > TIM wrote: ... > very strange to assign it to an int). ... > Neither of these functions are declared, so this should not compile. ... i just wanted to make programm such ARTMOBNEY if you heard of it... ...
    (comp.lang.cpp)
  • Re: solve 8x8 matrix
    ... I programm in C++. ... With a simple Gauss pivoting program, ... int main{ ... void gauss{ ...
    (sci.math.num-analysis)