Click to See Complete Forum and Search --> : CSocket thread


refleks17
June 4th, 2004, 11:19 PM
Hello I derived a class from the CSocket class. I'm trying to make the Send() function in a thread but the program is just freezing . Heres the code :


// declaration

class CPSock : public CSocket
{
DECLARE_DYNCREATE(CPSock)
// Attributes
public:
typedef struct SENDPACKAGE
{
CPSock* _this;
const void *lpBuf;
int nBufLen;
int nFlags;
} SENDPACKAGE;

typedef struct RECEIVEPACKAGE
{
CPSock* _this;
void* lpBuf;
int nBufLen;
int nFlags;
} RECEIVEPACKAGE;

//functions,,,
};


//IMPLEMENTATION (.cpp)

int CPSock::Send(const void* lpBuf, int nBufLen, int nFlags)
{
_param->_this = this;
_param->lpBuf = lpBuf;
_param->nBufLen = nBufLen;
_param->nFlags = nFlags;

AfxBeginThread (TrSend, _param);
return nBufLen;
}

int CPSock::SendNormal(const void* lpBuf, int nBufLen, int nFlags)
{
return CSocket::Send(lpBuf, nBufLen, nFlags);
}

UINT CPSock::TrSend(LPVOID param)
{
SENDPACKAGE* ts = (SENDPACKAGE*) param;

return ts->_this->SendNormal(ts->lpBuf,ts->nBufLen,ts->nFlags);
}


Anybody can help me out?

kuphryn
June 5th, 2004, 12:09 AM
Post the declaration of _param. Secondly, I doubt CSocket is thread-safe. If you want to use winsock wrapper, then check out CAsyncSocket.

Kuphryn

refleks17
June 5th, 2004, 12:36 AM
sorry, heres the declaration :
SENDPACKAGE _param;

CASyncSocket is blocking though :(

atif_ilm
June 5th, 2004, 01:40 AM
Originally posted by refleks17
sorry, heres the declaration :
SENDPACKAGE _param;

CASyncSocket is blocking though :(



CSocket .................Blocking Socket
CAsyncSocket.........Non Blocking Socket

atif_ilm
June 5th, 2004, 01:45 AM
Originally posted by refleks17
Hello I derived a class from the CSocket class. I'm trying to make the Send() function in a thread but the program is just freezing . Heres the code :


// declaration

class CPSock : public CSocket
{
DECLARE_DYNCREATE(CPSock)
// Attributes
public:
typedef struct SENDPACKAGE
{
CPSock* _this;
const void *lpBuf;
int nBufLen;
int nFlags;
} SENDPACKAGE;

typedef struct RECEIVEPACKAGE
{
CPSock* _this;
void* lpBuf;
int nBufLen;
int nFlags;
} RECEIVEPACKAGE;

//functions,,,
};


//IMPLEMENTATION (.cpp)

int CPSock::Send(const void* lpBuf, int nBufLen, int nFlags)
{
_param->_this = this;
_param->lpBuf = lpBuf;
_param->nBufLen = nBufLen;
_param->nFlags = nFlags;

AfxBeginThread (TrSend, _param);
return nBufLen;
}

int CPSock::SendNormal(const void* lpBuf, int nBufLen, int nFlags)
{
return CSocket::Send(lpBuf, nBufLen, nFlags);
}

UINT CPSock::TrSend(LPVOID param)
{
SENDPACKAGE* ts = (SENDPACKAGE*) param;

return ts->_this->SendNormal(ts->lpBuf,ts->nBufLen,ts->nFlags);
}


Anybody can help me out?


CSocket with worker thread has some seriouse problems , so why dont u try User Interface threads.

refleks17
June 5th, 2004, 01:51 AM
Well I don't figured out how to make CAsyncSocket with ftp connections... but CSocket works well.

kuphryn
June 5th, 2004, 02:25 AM
Create _param on the heap.

Kuphryn

refleks17
June 5th, 2004, 02:48 AM
Hello,

Thans for your reply. But what is the heap? I heard it has something to do with memory but i don't know more about what people call the heap.

refleks17
June 5th, 2004, 09:09 AM
Hello again,

I tried the malloc and free function but it doesn't work neither :(


CPSock::CPSock()
{
_param = new SENDPACKAGE;
_param = (SENDPACKAGE*)malloc(sizeof(_param));
}

CPSock::~CPSock()
{
free(_param);
}

Andreas Masur
June 6th, 2004, 05:18 AM
[Moved thread]