0

I have a Qt program that needs to use VC++ CAsyncSocket for TCP connection. I have created a dll. to wrap CAsyncSocket.

class CClientSocket ;
class __declspec(dllexport) CClientSocketEx
{
public:
    CClientSocketEx();
     ~CClientSocketEx();

     void Close();
     bool Create();
     bool Connect(const char* lpszHostAddress, unsigned int nHostPort); 
     int Send(const char* lpBuf, int nBufLen, int nFlags = 0);
     bool GetPeerName(char* rPeerAddress, int length, unsigned int& rPeerPort);
     int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
private:
    CClientSocket* clientSocket;
};

class CClientSocket : public CAsyncSocket
{
   virtual void OnReceive(int nErrorCode);
};

void CClientSocket::OnReceive(int nErrorCode)
{
    if (!AfxSocketInit())
    {
        AfxMessageBox(_T("Failed to Initialize Sockets"), MB_OK | MB_ICONSTOP);
    }
    CString mesage(_T("CClientSocket::OnReceive nErrorCode: %d"), nErrorCode);
    logMessage(mesage);
    CAsyncSocket::OnReceive(nErrorCode);
}

The problem I have is that CAsyncSocket::OnRecieve is never called.

Further, if I call CAsyncSocke::Recieve without waiting for the OnRecieve I get a WSAEWOULDBLOCK error.

When I unit test on my own PC everything works ok. When I try on client machine I have the above problem. The connect and send commands all work ok, and I am informed that the hardware is sending a reply, but no OnRecieve event.

sriep
  • 21
  • 5

1 Answers1

0

There appears to be a problem with sending the OnRecieve message in DLLs.

Despite the OnRecieve message not being sent, any response should still be available to be read by Recieve.

So a workaround seems to be, if using CAsyncSocket in a DLL, to not use CAsyncSocket::OnRecieve but only use CAsyncSocket::Receive. Perhaps attached to a timer.

sriep
  • 21
  • 5