CAsyncSocket Callbacks [Help!!!!] 
Author Message
 CAsyncSocket Callbacks [Help!!!!]

Hi,

I'm writting an ActiveX control that
opens a TCP connnection using CAsyncSocket
class, sends a stream and waits for a server
server response.

I want to wait some time while the connection
is being open. So, a read the open connection timeout
from a parameter file and invoke the connect method.
After this I use the WaitForSingleObject function to
to wait for the server response or the elapsed timeout.

Inside the CAsyncSocket derived class I have a HANDLE
that is going to be signaled by the OnConnect callback
function. My problem is that the OnConnect callback function
is never called by the framework. Due this, the timeout always
expired and my ActiveX thinks that the connection never was
opened (the most of the case this was opened suscessfully).

My question is:
why the OnConnect callback function is never called?
do I need to make any special initialization?
what I'm doing wrong?

Any help, or comment will be really appreciated.
Below I added part of my code. Please check it.

*** BEGIN OF CODE ***

** METHOD OF ActiveX CONTROL **
short CSocketCommCtrl::MakeComm()
{
  // TODO: Add your dispatch handler code here
  CString               MessageBuffer;
  MessageBuffer.Empty ();

  // Create the Log File Object
  m_pLogFile = new CLogFile;

  // Init the WinSocket Underlaying System
  WSADATA wsaData;

  if (!AfxSocketInit(&wsaData))
  {
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Error on Initialization
of WinSocket Underlaying System: %d", GetLastError ());
    m_strLastError.Format ("Error on Initialization of WinSocket
Underlaying System: %d", GetLastError ());
    delete m_pLogFile;

    m_outputParameter.Empty ();
    m_numLastError = -1;
    return (-1);
  }

  if (m_debugLevel > 0)
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Initialization of
WinSocket Underlaying System Complete.");

  // Create the Socket Device
  m_pSocket = new CSocketDevice;

  // Fill the Socket Data
  m_pSocket->m_serverAddress =   m_serverAddress;
  m_pSocket->m_serverPort = m_serverPort;
  m_pSocket->m_bytesToRead = m_streamLength;
  m_pSocket->m_replyTimeout = m_replyTimeout;
  m_pSocket->m_connectionTimeout = m_connectionTimeout;
  m_pSocket->m_debugLevel = m_debugLevel;
  m_pSocket->m_openconnectontimeOutEvent = ::CreateEvent (NULL, TRUE,
FALSE, NULL);
  m_pSocket->m_listenerthreadtimeOutEvent = ::CreateEvent (NULL, TRUE,
FALSE, NULL);
  m_pSocket->m_listenerthreadendEvent = ::CreateEvent (NULL, TRUE,
FALSE, NULL);
  ::ResetEvent (m_pSocket->m_openconnectontimeOutEvent);
  ::ResetEvent (m_pSocket->m_listenerthreadtimeOutEvent);
  ::ResetEvent (m_pSocket->m_listenerthreadendEvent);

  // Create the Socket
  if (!m_pSocket->Create (0, SOCK_STREAM, FD_READ | FD_WRITE |
FD_CONNECT))
  {
    int LastError = m_pSocket->GetLastError ();
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Error on Creating Socket:
%d", LastError);
    m_strLastError.Format ("Error on Creating Socket: %d", LastError);
    m_outputParameter.Empty ();
    m_numLastError = -1;
    delete m_pLogFile;
    ::CloseHandle (m_pSocket->m_listenerthreadendEvent);
    ::CloseHandle (m_pSocket->m_listenerthreadtimeOutEvent);
    ::CloseHandle (m_pSocket->m_openconnectontimeOutEvent);
    delete m_pSocket;
    return -1;
  }

  if (m_debugLevel > 0)
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Socket Object Created.");

  // Connect to the Socket
  m_pSocket->AsyncSelect (FD_CONNECT);
  if ((!m_pSocket->Connect(m_serverAddress, m_serverPort)) &&
(m_pSocket->GetLastError () != WSAEWOULDBLOCK))
  {
    int LastError = m_pSocket->GetLastError ();
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Error Connecting Socket.
Server: %s, Port: %d, LastError: %d", m_serverAddress, m_serverPort,
LastError);
    m_strLastError.Format ("Error Connecting Socket. Server: %s, Port: %
d, LastError: %d", m_serverAddress, m_serverPort, LastError);
    ::CloseHandle (m_pSocket->m_listenerthreadendEvent);
    ::CloseHandle (m_pSocket->m_listenerthreadtimeOutEvent);
    ::CloseHandle (m_pSocket->m_openconnectontimeOutEvent);
    m_outputParameter.Empty ();
    m_numLastError = -1;
    delete m_pLogFile;
    delete m_pSocket;
    return -1;
  }

  // Wait Here Connection Timeout or Open Connection Event
  DWORD WaitStatus = ::WaitForSingleObject (m_pSocket-

Quote:
>m_openconnectontimeOutEvent, m_connectionTimeout*1000);

  // Check what Happend
  if (WaitStatus != WAIT_OBJECT_0)
  {
    // The Connection Timeout Expired, Abort Process
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Error Connecting Socket.
Server: %s, Port: %d, Connection Timeout Expired", m_serverAddress,
m_serverPort);
    m_strLastError.Format ("Error Connecting Socket. Server: %s, Port: %
d, Connection Timeout Expired", m_serverAddress, m_serverPort);
    ::CloseHandle (m_pSocket->m_listenerthreadendEvent);
    ::CloseHandle (m_pSocket->m_listenerthreadtimeOutEvent);
    ::CloseHandle (m_pSocket->m_openconnectontimeOutEvent);
    m_outputParameter.Empty ();
    m_numLastError = -1;
    m_pSocket->Close ();
    delete m_pLogFile;
    delete m_pSocket;
    return -1;
  }

  // The Connection is Open
  if (m_debugLevel > 0)
    m_pLogFile->WriteLog (0, 0, WITHDATE, 1, "Socket Connection Open.
Server: %s, Port: %d", m_serverAddress, m_serverPort);

  // Get the Message to Be Sended
  CString OutputMessage;
  OutputMessage.Empty ();
  OutputMessage = m_inputParameter;

  // Send Data
  .
  .
  .

  // Receive Data
  .
  .
  .

  // Close the Handles
  .
  .
  .

  // Get the Received Buffer
  m_outputParameter.Empty ();
  m_outputParameter = m_pSocket->m_socketStream;
  m_numLastError = 0;

  .
  .
  .

  // Close the Communication
  m_pSocket->Close ();

  // Delete Socket Object
  delete m_pSocket;

  m_strLastError = "Interface No Errors";
  delete m_pLogFile;
  return 0;

Quote:
}

** ONCONNECT CALLBACK FUNCTION **
void CSocketDevice::OnConnect(int nErrorCode)
{
  // TODO: Add your specialized code here and/or call the base class
  // Sign Connection Complete Event
  ::SetEvent (m_openconnectontimeOutEvent);

  CAsyncSocket::OnConnect(nErrorCode);

Quote:
}

***  END OF CODE  ***

Thanks in advance,
Marc Bonet

--
Is better to know useless things than
don't know anything at all

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 20 Apr 2003 06:04:56 GMT  
 CAsyncSocket Callbacks [Help!!!!]

Quote:

> Hi,

> I'm writting an ActiveX control that
> opens a TCP connnection using CAsyncSocket
> class, sends a stream and waits for a server
> server response.

> I want to wait some time while the connection
> is being open. So, a read the open connection timeout
> from a parameter file and invoke the connect method.
> After this I use the WaitForSingleObject function to
> to wait for the server response or the elapsed timeout.

> Inside the CAsyncSocket derived class I have a HANDLE
> that is going to be signaled by the OnConnect callback
> function. My problem is that the OnConnect callback function
> is never called by the framework. Due this, the timeout always
> expired and my ActiveX thinks that the connection never was
> opened (the most of the case this was opened suscessfully).

> My question is:
> why the OnConnect callback function is never called?
> do I need to make any special initialization?
> what I'm doing wrong?

> Any help, or comment will be really appreciated.
> Below I added part of my code. Please check it.

You are blocking in what should be an event-driven environment.  The
CAsyncSocket OnConnect callback is called as a result of a windows message.  So
in order for it to work your app must be processing the message queue.  

Instead, you are calling WaitForSingleObject, which stops everything (unless
there are other threads here that you did not mention).  OnConnect cannot get in
because you did not return to the MFC messaage dispatching code.  After calling
Connect simply return so MFC will process messages.  Then when the OnConnect
message comes in you will receive the callback.

--
Scott McPhillips [VC++ MVP]



Sun, 20 Apr 2003 10:14:30 GMT  
 CAsyncSocket Callbacks [Help!!!!]


Quote:

> > Hi,

> > I'm writting an ActiveX control that
> > opens a TCP connnection using CAsyncSocket
> > class, sends a stream and waits for a server
> > server response.

> > I want to wait some time while the connection
> > is being open. So, a read the open connection timeout
> > from a parameter file and invoke the connect method.
> > After this I use the WaitForSingleObject function to
> > to wait for the server response or the elapsed timeout.

> > Inside the CAsyncSocket derived class I have a HANDLE
> > that is going to be signaled by the OnConnect callback
> > function. My problem is that the OnConnect callback function
> > is never called by the framework. Due this, the timeout always
> > expired and my ActiveX thinks that the connection never was
> > opened (the most of the case this was opened suscessfully).

> > My question is:
> > why the OnConnect callback function is never called?
> > do I need to make any special initialization?
> > what I'm doing wrong?

> > Any help, or comment will be really appreciated.
> > Below I added part of my code. Please check it.

> You are blocking in what should be an event-driven environment.  The
> CAsyncSocket OnConnect callback is called as a result of a windows
message.  So
> in order for it to work your app must be processing the message
queue.

> Instead, you are calling WaitForSingleObject, which stops everything
(unless
> there are other threads here that you did not mention).  OnConnect
cannot get in
> because you did not return to the MFC messaage dispatching code.
After calling
> Connect simply return so MFC will process messages.  Then when the
OnConnect
> message comes in you will receive the callback.

> --
> Scott McPhillips [VC++ MVP]

Hi Scott,

thanks for your quick and accurate answer.
I understand your point of check the message queue,
however I have a problem and don't know how to solve:

My activeX control is a windowless control, so
the m_hWnd member variables is always 0x0000000
and I can't receive (don't know how) message.
I have test with several message queue functions
to receive the callback and found that if I Post
a message (WM_USER or whatever) with the PostMessage
I receive the callback. The problem is that after this
I get a assertion fault cause in

_AFXWIN_INLINE BOOL CWnd::PostMessage(UINT message, WPARAM wParam,
LPARAM lParam)
        { ASSERT(::IsWindow(m_hWnd)); return ::PostMessage(m_hWnd,
message, wParam, lParam); }

this is the MFC source file AFXWIN2.INL line 41.

This problem is due that m_hWnd is NULL [windowless control]...
what can I do?

Thanks in advance for any help or comment,
Marc
--
Is better to know useless things than
don't know anything at all

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 21 Apr 2003 00:01:13 GMT  
 CAsyncSocket Callbacks [Help!!!!]

Quote:

> thanks for your quick and accurate answer.
> I understand your point of check the message queue,
> however I have a problem and don't know how to solve:

> My activeX control is a windowless control, so
> the m_hWnd member variables is always 0x0000000
> and I can't receive (don't know how) message.
> I have test with several message queue functions
> to receive the callback and found that if I Post
> a message (WM_USER or whatever) with the PostMessage
> I receive the callback. The problem is that after this
> I get a assertion fault cause in

> _AFXWIN_INLINE BOOL CWnd::PostMessage(UINT message, WPARAM wParam,
> LPARAM lParam)
>         { ASSERT(::IsWindow(m_hWnd)); return ::PostMessage(m_hWnd,
> message, wParam, lParam); }

> this is the MFC source file AFXWIN2.INL line 41.

> This problem is due that m_hWnd is NULL [windowless control]...
> what can I do?

> Thanks in advance for any help or comment,
> Marc

Not sure, but some random suggestions: Try a windowed version of the control.
To get around the above assertion you could ::PostMessage.  That simply bypasses
MFC code.  CAsyncSocket creates its own window, something like m_SocketWnd.
Perhaps you could get at that and use its HWND.  Finally, something that will
definitely work cleanly is to create and run the CAsyncSocket in an MFC
user-interface thread.

--
Scott McPhillips [VC++ MVP]



Mon, 21 Apr 2003 09:20:21 GMT  
 CAsyncSocket Callbacks [Help!!!!]


Quote:


> > thanks for your quick and accurate answer.
> > I understand your point of check the message queue,
> > however I have a problem and don't know how to solve:

> > My activeX control is a windowless control, so
> > the m_hWnd member variables is always 0x0000000
> > and I can't receive (don't know how) message.
> > I have test with several message queue functions
> > to receive the callback and found that if I Post
> > a message (WM_USER or whatever) with the PostMessage
> > I receive the callback. The problem is that after this
> > I get a assertion fault cause in

> > _AFXWIN_INLINE BOOL CWnd::PostMessage(UINT message, WPARAM wParam,
> > LPARAM lParam)
> >         { ASSERT(::IsWindow(m_hWnd)); return ::PostMessage(m_hWnd,
> > message, wParam, lParam); }

> > this is the MFC source file AFXWIN2.INL line 41.

> > This problem is due that m_hWnd is NULL [windowless control]...
> > what can I do?

> > Thanks in advance for any help or comment,
> > Marc

> Not sure, but some random suggestions: Try a windowed version of the
control.
> To get around the above assertion you could ::PostMessage.  That
simply bypasses
> MFC code.  CAsyncSocket creates its own window, something like
m_SocketWnd.
> Perhaps you could get at that and use its HWND.  Finally, something
that will
> definitely work cleanly is to create and run the CAsyncSocket in an
MFC
> user-interface thread.

> --
> Scott McPhillips [VC++ MVP]

Thank you very much Scott,
I'll make some test to figure out
how to avoid the assertion failure.
I'll keep you inform...

Best regards,
Marc
--
Is better to know useless things than
don't know anything at all

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 21 Apr 2003 19:27:36 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Pls Help - Delay in CAsyncSocket Callbacks

2. Recieving callback message from CAsyncSocket

3. CAsyncSocket callbacks in threads

4. CALLBACK, CALLBACK, CALLBACK?

5. Help CAsyncSocket In Thread

6. CAsyncSocket help greatly appreciated !

7. HELP! CAsyncSocket problems

8. Need help about CAsyncSocket...

9. CAsyncSocket:SendTo() Error - Help

10. Help: Use a particular NIC through CAsyncSocket

11. Help CAsyncSocket In Thread

12. help with CAsyncSocket

 

 
Powered by phpBB® Forum Software