# # ifndef _CTHREADS_H_
# define _CTHREADS_H_
#include
#include
#include
#include
using namespace std;
The class CThreadBase
{
Public:
CThreadBase () {};
CThreadBase (SOCKET& The socket);
Virtual ~ CThreadBase ();
Bool StartThread ();
Bool EndThread ();
The static DWORD __stdcall CreateTread (void * pThreaPrams);
Private:
Virtual DWORD ThreadFun ();
Protected:
HANDLE m_ThreadHandle;
LPDWORD m_ThreadID;
The SOCKET m_Socket;
};
The class CSocketThread: public CThreadBase
{
Public:
CSocketThread (SOCKET& The socket) : CThreadBase (socket) {};
Virtual ~ CSocketThread ();
Private:
Virtual DWORD ThreadFun ();
};
# endif//_CTHREADS_H_
2. CThreads. CPP
# include "StdAfx. H"
# include "CThreads. H"
CThreadBase: : CThreadBase (SOCKET& The socket)
{
M_ThreadHandle=NULL;
M_ThreadID=NULL;
M_Socket=socket;
}
CThreadBase: : ~ CThreadBase ()
{
If (m_ThreadHandle)
{
: : TerminateThread (m_ThreadHandle, 0);
: : WaitForSingleObject (m_ThreadHandle, 500);
: : CloseHandle (m_ThreadHandle);
}
M_ThreadHandle=NULL;
M_ThreadID=NULL;
}
Bool CThreadBase: : StartThread ()
{
If (NULL!=m_ThreadHandle)
{
return true;
}
M_ThreadHandle=: : CreateThread (NULL, 0, CThreadBase: : CreateTread, (void *) this, 0, m_ThreadID);
If (NULL==m_ThreadHandle)
{
Unsigned long uErrNo=: : GetLastError ();
Printf (" Create Thread Fail. ErrNo: % d ", uErrNo);
}
Return NULL!=m_ThreadHandle;
}
Bool CThreadBase: : EndThread ()
{
If (m_ThreadHandle)
{
: : TerminateThread (m_ThreadHandle, 0);
: : WaitForSingleObject (m_ThreadHandle, 500);
: : CloseHandle (m_ThreadHandle);
}
M_ThreadHandle=NULL;
M_ThreadID=NULL;
return true;
}
DWORD __stdcall CThreadBase: : CreateTread (void * pThreaPrams)
{
PThreaPrams CThreadBase * pThread=(CThreadBase *);
Return the pThread - & gt; ThreadFun ();
}
DWORD CThreadBase: : ThreadFun ()
{
# define BUFFERLEN 2048
Char cBuf [BUFFERLEN];
Int iRevDataLen=: : recv (m_Socket, cBuf, BUFFERLEN, 0);
//Do the action after recive data
Printf (" CThreadBase: : ThreadFun recdate: % s ", cBuf);
//send the result
return 0;
}
CSocketThread: : ~ CSocketThread ()
{
};
DWORD CSocketThread: : ThreadFun ()
{
# define BUFFERLEN 2048
Char cBuf [BUFFERLEN];
Int iRevDataLen=: : recv (m_Socket, cBuf, BUFFERLEN, 0);
//Do the action after recive data
Printf (" CSocketThread: : ThreadFun recdate: % s ", cBuf);
//send the result
return 0;
}
3. The main function
Int _tmain (int arg c, _TCHAR * argv [])
{
.
//to establish a Socket connection
Int clientaddrlen=sizeof (clientaddr);
Clientsock=accept (ServerSock, (struct sockaddr *) & amp; Clientaddr, & amp; Clientaddrlen);
//multi-threaded processing new Socket
CSocketThread socketThread (clientsock);
SocketThread. StartThread ();
SocketThread. EndThread ();
......
}
Question:
Why object inside threads start function, did not get the client to come over data?
Also is no output CSocketThread: : ThreadFun recdate:,
What a great god help me answer the ah, urgent urgent
CodePudding user response:
Fyi://loop to a function every time send 200 bytes (this is the fixed) buffer,
//a function need to incoming buffer, the circulation of 240 bytes (fixed) new buffer for processing,
//when dealing with each new printing takes two bytes of the buffer
# ifdef _MSC_VER
# pragma warning (4996) disable:
# endif
#include
#include
#include
# ifdef _MSC_VER
#include
#include
#include
# define MYVOID void
# define vsnprintf _vsnprintf
# the else
#include
#include
#include
# define CRITICAL_SECTION pthread_mutex_t
# define MYVOID void *
# endif
//the Log {
# define MAXLOGSIZE 20000000
# define MAXLINSIZE 16000
#include
#include
#include
Char logfilename1 []="MyLog1. Log";
Char logfilename2 []="MyLog2. Log";
The static char logstr [MAXLINSIZE + 1];
Char datestr [16].
Char timestr [16].
Char MSS [4].
CRITICAL_SECTION cs_log;
The FILE * flog;
# ifdef _MSC_VER
The Lock (CRITICAL_SECTION void * l) {
The EnterCriticalSection (l);
}
Void Unlock (CRITICAL_SECTION * l) {
LeaveCriticalSection (l);
}
Void sleep_ms (int) ms {
Sleep (ms).
}
# the else
The Lock (CRITICAL_SECTION void * l) {
Pthread_mutex_lock (l);
}
Void Unlock (CRITICAL_SECTION * l) {
Pthread_mutex_unlock (l); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull