cURL / Mailing Lists / curl-library / Single Mail

curl-library

Libcurl dalays SYN (HTTP connection timeout)

From: conchi <conchi.felipe_at_lanaccess.es>
Date: Tue, 27 Apr 2010 10:27:50 +0200

Hello again,

Maybe I have not explained well the problem.

I have made a test program that make a HTTP request to a Web server.

 

I invoke the curllib with two timeouts:

 

curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT_MS, 2000);

curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT_MS, 2000);

 

Always invoke the curlib with the remote IP address (not by name)

I have a loop making this request all the time.

 

Sometimes the Curl_connecthost function returns an CURLE_COULDNT_CONNECT
error.

I sniff the network with Lansleuth (or with wireshark) and I see that the
SYN is delays about 4 seconds, so when the socket is connected the timeout
is expired and the curlib closes the socket.

 

I can attach the file from wireshark. You can see that the SYN usually are
in the same second, but the last SYN has been delayed 5 seconds and the
curlib has closed the socket when it has connected.

My PC is the 172.16.0.144. The Web server is 172.16.223.4.

My code is very simple:

 

m_http.OpenSession(m_strIP,80,"hello","world");

int res;

ULONG ulContadorTotal=0;

ULONG ulCntFallos=0;

      

do

{

      res=m_http.Request(str,2000);

      if(res==-1)

      {

            ulCntFallos++;

            TRACE("[Trying %d] ERROR\n",ulContadorTotal);

      }

      else if(res==-2) //es otro tipo de error de HTTP.

      {

            ulCntFallos++;

            TRACE("[Trying %d] ERROR\n",ulContadorTotal);

      }

      else

      {

            TRACE("[Trying %d] OK\n",ulContadorTotal);

      }

      ulContadorTotal++;

}while(ulContadorTotal<500);

 

 

 

int CMyHTTPClientCURL::OpenSession(LPCTSTR szAddress, USHORT uPort, LPCTSTR
szUsername, LPCTSTR szPassword, UINT
uiMsTimeout/*=DEFAULT_HTTPCONNECT_TIMEOUT*/)

{

      CString strUsrPwd;

 

      if (m_pCURL != NULL)

      {

            strUsrPwd.Format("%s:%s", szUsername, szPassword);

 

            curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, strUsrPwd);

 

            curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT_MS, uiMsTimeout);

            curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT_MS,
uiMsTimeout);

 

            /* send all data to this function */

            curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

            /* we pass our 'chunk' struct to the callback function */

            curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, (void
*)&m_stChunk);

      }

 

      m_sAddress = szAddress;

      m_iPort = uPort;

      m_strUsername= szUsername;

      m_strPassword= szPassword;

 

      return 0;

}

 

 

// Pide una conexion HTTP

int CMyHTTPClientCURL::Request(LPCTSTR szPath, UINT
uiMsTimeout/*=DEFAULT_HTTPCURL_CONNECT_TIMEOUT*/)

{

      CURLcode curl_res;

      int iResult = 0;

      CString sURL;

 

      sURL.Format(_T("http://%s:%d"), m_sAddress, m_iPort);

      sURL.AppendFormat(szPath[0] == _T('/') ? _T("%s") : _T("/%s"),
szPath);

 

      // Init variables for the new request

      curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT_MS, uiMsTimeout);

      curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT_MS, uiMsTimeout);

      ResetResponse();

      m_dwLastHttpError = 0;

 

      // Proccess request

      if (m_pCURL != NULL)

      {

            sURL.Replace(_T(" "), _T("%20"));

            curl_easy_setopt(m_pCURL, CURLOPT_URL, sURL);

            curl_res = curl_easy_perform(m_pCURL);

      }

 

      // Errors ?

 

      switch (curl_res)

      {

      case CURLE_PARTIAL_FILE:

      case CURLE_OK:

            m_pReadPosition = m_stChunk.memory;

            

            // Obtener el codigo de error devuelto por el servidor

            curl_easy_getinfo(m_pCURL, CURLINFO_RESPONSE_CODE,
&m_dwLastHttpError);

 

            iResult = m_dwLastHttpError;

            if ((m_dwLastHttpError / 100) != 2)

            {

                  iResult = -2;

                  TRACE("HTTPClientCURL Error: %d \n",m_dwLastHttpError);

            }

            else

            {

                  TRACE("HTTPClientCURL OK \n");

            }

            break;

      default:

            // Se han producido errores que no son HTTP (socket, buffer,
etc)

            iResult = -1;

            m_dwLastHttpError = curl_res;

            TRACE("HTTPClientCURL Error : %d\n", m_dwLastHttpError);

            break;

      }

 

      return iResult;

}

 

 

Sorry for my bad English.

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html

Received on 2010-04-27