question

Greg-4839 avatar image
0 Votes"
Greg-4839 asked JeanineZhang-MSFT commented

How do you use CAsyncSocket to make a HTTP request and read the response?

This is where I attempt to send the HTTP request.

class CIrrigationHTTPSocket is derived from CAsyncSocket, and I am simply calling the ::Connect(...) function.

But it generates the error "This socket has not been bound with Bind or is already connected." but I have no idea what this means or how to resolve it.


 bool CIrrigationHTTPSocket::Send(LPCSTR lpszData)
 {
     int nResult = 0;
     CString strData;
     int nLength = 0;
    
     if (m_strDestIPAddr.GetLength() > 0)
     {
         if (!m_strPassword.IsEmpty())
             strData = CString("windows") + m_strDelim + m_strPassword + m_strDelim + lpszData + m_strDelim;
         else
             strData = CString("windows") + m_strDelim + lpszData + m_strDelim;
    
         encodeBase64(strData);
         strData = m_strDestIPAddr + "/" + strData;
         nLength = strData.GetLength();
         m_nTimeoutTrigger = GetTickCount() + m_nWait;
         if (!Connect(strData, 80))
         {
             DisplayError(GetLastError());
             nResult = 0;
         }
     }
     return nResult > 0;
 }

This is how I have setup this socket:

 bool CIrrigationHTTPSocket::Create()
 {
     BOOL bResult = CAsyncSocket::Create();
     if (!bResult)
         DisplayError(GetLastError());
     Initialise();
    
     if (!Bind(m_nLocalListenPort, m_strLocalIPAddr))
         DisplayError(GetLastError());
    
     return bResult == TRUE;
 }
c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Greg-4839

As far as I 'm concerned, the network operations are potentially time-consuming operations. And since you're using MFC's CAsyncSocket class, it performs all the operations asynchronously (doesn't block you). But return from the function doesn't mean it's already completed. I suggest you could start sending after OnConnect has been called.


0 Votes 0 ·

0 Answers