Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CSocket - Timeout with MFC

726 views
Skip to first unread message

Alex Basler

unread,
Mar 31, 2002, 10:57:54 AM3/31/02
to
Hello,

I need a function, where can I define a Timeout.

What I want/need?

Send Something
Start Timer
If Timeout I get a message from the function.

Receive.

In MSDN:

Berkeley Software Distribution (BSD) options not supported for SetSockOpt
are:
SO_SNDTIMEO int Send timeout.


BOOL bReturn = SetSockOpt( int nOptionName, SO_SNDTIMEO , int nOptionLen,
int nLevel = SOL_SOCKET );

Can you give me a example?
Thanks al lot.

--
Best regards
Alex


Scott McPhillips

unread,
Mar 31, 2002, 1:44:22 PM3/31/02
to

CSocket implements a blocking socket, meaning you can't do anything else
in the same thread while the socket is in the Send call. If you use
CAsyncSocket instead you can do anything you want, such as count
WM_TIMER messages.

--
Scott McPhillips [VC++ MVP]

Alex Basler

unread,
Apr 1, 2002, 1:54:12 AM4/1/02
to
Hello Scott,

thanks for your answer.

> > In MSDN:
> >
> > Berkeley Software Distribution (BSD) options not supported for
SetSockOpt
> > are:
> > SO_SNDTIMEO int Send timeout.
> >
> > BOOL bReturn = SetSockOpt( int nOptionName, SO_SNDTIMEO , int
nOptionLen,
> > int nLevel = SOL_SOCKET );

> CSocket implements a blocking socket, meaning you can't do anything else
> in the same thread while the socket is in the Send call. If you use
> CAsyncSocket instead you can do anything you want, such as count
> WM_TIMER messages.

Can you give me a example, because I didn't have expierence with
CAsyncSocket?
Thanks a lot.

--
Best regards
Alex

Alex Basler

unread,
Apr 1, 2002, 1:06:57 PM4/1/02
to
Hello Scott,

here are my class.


BR Alex

class CMySocket : public CAsyncSocket
{
// Attributes
public:

// Operations
public:
CMySocket();
virtual ~CMySocket();

// Overrides
public:
void SetConnStatus(const BOOL bConn);
BOOL GetConnStatus() const;
virtual void OnReceive(int nErrorCode);
void SetParent(CDialog *pWnd);
// ClassWizard generated virtual function overrides
file://{{AFX_VIRTUAL(CMySocket)
file://}}AFX_VIRTUAL

// Generated message map functions
file://{{AFX_MSG(CMySocket)
// NOTE - the ClassWizard will add and remove member functions here.
file://}}AFX_MSG

// Implementation
protected:
private:
BOOL m_bIsConected;
CDialog* m_pWnd;
};


////////////////////////////////////////////////////////////////////////////
/
// CMySocket

CMySocket::CMySocket()
{
}

CMySocket::~CMySocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CMySocket, CAsyncSocket)
file://{{AFX_MSG_MAP(CMySocket)
file://}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

////////////////////////////////////////////////////////////////////////////
/
// CMySocket member functions

void CMySocket::SetParent(CDialog *pWnd)
{
m_pWnd = pWnd;
}


void CMySocket::OnReceive(int nErrorCode)
{
((CClientDlg*)m_pWnd)->OnReceive();
}

BOOL CMySocket::GetConnStatus() const
{
return m_bIsConected;
}

void CMySocket::SetConnStatus(const BOOL bConn)
{
m_bIsConected = bConn;
}

void CClientDlg::OnButsenden()
{
// TODO: Add your control notification handler code here
int iSend;
UpdateData(TRUE);

iSend = m_strNAchricht.GetLength();
m_ConnectSocket.Send(LPCTSTR(m_strNAchricht),iSend);
}

Alex Sidorov

unread,
Apr 1, 2002, 3:00:52 PM4/1/02
to
if you create your own threads, then it might look like this:

//to disconnect a hanging socket on time out
void CALLBACK OnTimer( HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
theApp.m_pMainWnd->KillTimer(idEvent);

((CYahooDlg*)theApp.m_pMainWnd)->Threads.Objects[idEvent].Socket.CancelBlock
ingCall();
}


//a Thread's body
UINT ThreadProc(LPVOID pParam)
{
CThread* pThread = (CThread*)pParam;
pThread->Socket.Create();
while(!pThread->Terminated)
{
CS.Lock();
theApp.m_pMainWnd->SetTimer(pThread->Index,theApp.TimeOut,OnTimer);
CS.Unlock();
if(pThread->Socket.Connect(theApp.Host,theApp.Port))
{
if(pThread->Socket.Send( pThread->Request,
(pThread->Request).GetLength() ) != SOCKET_ERROR)
if(pThread->Socket.Receive( Response, RESPONSE_BUFFER_SIZE ) !=
SOCKET_ERROR)
{
theApp.m_pMainWnd->KillTimer(pThread->Index);
//process response data here
}
}
pThread->Socket.Close();
}
pThread->Socket.Close();
CS.Lock();
if(--pThread->pParent->Count==0) //the last thread is done
{
theApp.OnWorkDone();
theApp.m_pMainWnd->KillTimer(pThread->Index);
}
CS.Unlock();
return 0;
}

Alex Basler

unread,
Apr 2, 2002, 2:04:03 PM4/2/02
to
Hello Alex,

Thank for the example.
Can you still explain this to me?
Object CS.Lock() ?
How I start the Thread?
How I end the Thread?

Or you send a small project to me?
Thanks.

Best regards Alex

"Alex Sidorov" <gr...@mail.ru> schrieb im Newsbeitrag
news:#87F9eb2BHA.1228@tkmsftngp04...


> if you create your own threads, then it might look like this:
>

> file://to disconnect a hanging socket on time out


> void CALLBACK OnTimer( HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime )
> {
> theApp.m_pMainWnd->KillTimer(idEvent);
>
>
((CYahooDlg*)theApp.m_pMainWnd)->Threads.Objects[idEvent].Socket.CancelBlock
> ingCall();
> }
>
>

> file://a Thread's body


> UINT ThreadProc(LPVOID pParam)
> {
> CThread* pThread = (CThread*)pParam;
> pThread->Socket.Create();
> while(!pThread->Terminated)
> {
> CS.Lock();
> theApp.m_pMainWnd->SetTimer(pThread->Index,theApp.TimeOut,OnTimer);
> CS.Unlock();
> if(pThread->Socket.Connect(theApp.Host,theApp.Port))
> {
> if(pThread->Socket.Send( pThread->Request,
> (pThread->Request).GetLength() ) != SOCKET_ERROR)
> if(pThread->Socket.Receive( Response, RESPONSE_BUFFER_SIZE ) !=
> SOCKET_ERROR)
> {
> theApp.m_pMainWnd->KillTimer(pThread->Index);

> file://process response data here


> }
> }
> pThread->Socket.Close();
> }
> pThread->Socket.Close();
> CS.Lock();

> if(--pThread->pParent->Count==0) file://the last thread is done

Alex Sidorov

unread,
Apr 2, 2002, 7:28:39 PM4/2/02
to
CS.Lock() is a call to CCriticalSection::Lock() to prevent some
thread-unsafe actions on data.

void CThreads::Start()
{
for(int i=0; i<theApp.ThreadsNum; i++)
{
Items[i] =
AfxBeginThread(ThreadProc,&Objects[i],theApp.Priority,0,CREATE_SUSPENDED);
Items[i]->m_bAutoDelete = TRUE;
Count++;
}
for(i=0; i<theApp.ThreadsNum; i++) Items[i]->ResumeThread();
}

void CThreads::Pause()
{
for(int i=0; i<theApp.ThreadsNum; i++)
Items[i]->SuspendThread();
}

void CThreads::Resume()
{
for(int i=0; i<theApp.ThreadsNum; i++)
Items[i]->ResumeThread();
}
To end the thread, call AfxEndThread from within the thread, or return from
the controlling function of the worker thread.
never use TerminateThread()!

I don't have small projects, sorry, all ones I have are close-source.

"Alex Basler" <so...@planet-interkom.de> wrote in message
news:a8cvdg$rmf$1...@newsread1.arcor-online.net...

0 new messages