IdHTTP->ConnectTimeout works in Windows 7, does not work on XP

1.5k views Asked by At

Using Embarcadero C++ Builder 2010, I'm trying to detect specific web server devices connected to a network.

I'm interrogating those devices by calling a CGI function using TIdHTTP and analyzing the answer on a range of IP address.

I am using IdHTTP->ConnectTimeout and IdHTTP->ReadTimeout parameters to throws an exception if there's no connection by the time the timeout elapses. This should happen when TIdHTTP is interrogating an unexpected devices, in this case I release IdHTTP by calling IdHTTP->Free().

It works fine on Windows 7, but on Windows XP the timeout doesn't apply, so it can take up to 10 minutes before IdHTTP.get() stop.

By removing those two parameters the app work a bit faster on Windows XP but still take to long compare to the timeout than I am expecting.

I found this related subject without real solutions: Indy 10 + SSL = works in Windows 7, does not work on XP

This is a part of the source code:

        #include "winsock2.h"

        String url = "http://" + anIpAddress + "/myCGIFunction.cgi";
        TMemoryStream *XML = new TMemoryStream;
        AnsiString answer="";

        p_IdHTTP = new TIdHTTP(NULL);
        try
        {
                p_IdHTTP->ConnectTimeout = 2000;

                p_IdHTTP->Get(url,XML);

                XML->Position = 0;
            answer=ReadStringFromStream(XML);

                p_IdHTTP->Free();
         }
         catch (...)
         {
            p_IdHTTP->Free();
         }

My questions are: Why does IdHTTP->ConnectTimeout isn't working on Windows XP? And do you have any idea to replace this parameter to release/destroyed the IdHTTP after a specific time?

Update:

    void __fastcall Func::MyFunc()
    {
                AnsiString anIpAddress= "20.20.20.11";
                String url = "http://" + anIpAddress + "/myCGIFunction.cgi";
                TMemoryStream *XML = new TMemoryStream;
                AnsiString answer="";

                p_IdHTTP = new TIdHTTP(NULL);
                try
                {
                        p_SSLHandler=new TIdSSLIOHandlerSocketOpenSSL(p_IdHTTP);
                p_SSLHandler->SSLOptions->Method= sslvSSLv23;

                p_IdHTTP->ConnectTimeout = 2000;
                p_IdHTTP->ReadTimeout=400;
                p_IdHTTP->IOHandler=p_SSLHandler;

                p_IdHTTP->OnConnected= IdHTTPConnected;
                p_IdHTTP->OnStatus= IdHTTPStatus;

                p_IdHTTP->Get(url,XML);
                        XML->Position = 0;
                    answer=ReadStringFromStream(XML);

                        p_IdHTTP->Free();
                 }
                 catch (...)
                 {
                    p_IdHTTP->Free();
                 }
   } 


void __fastcall Func::IdHTTPStatus(TObject *Sender, TIdStatus const AStatus, String const AStatusText){

    String msgtest = ipAddress ;

    switch (AStatus){
        case  (hsResolving):
            msgtest+= " resolving...";

            break;
        case  (hsConnecting):
            msgtest+= "  connecting...";
            break;
        case  (hsConnected):
            msgtest+= "  connected...";
            break;
        case  (hsDisconnecting):
            msgtest+= "  disconnecting...";
            break;
        case  (hsDisconnected):
            msgtest+= "  disconnected...";
            break;
        case  (hsStatusText):
            msgtest+= "  hsStatusText: " + AStatusText;
            break;
        default:
            msgtest+= " Other status ";
            break;
    }
    aListBox->Items->Add(msgtest);
    Application->ProcessMessages();
}

void __fastcall Func::IdHTTPConnected(TObject *Sender){
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;

setsockopt((unsigned int) p_IdHTTP->Socket->Binding, SOL_SOCKET,    
                           SO_RCVTIMEO, (char *)&timeout, sizeof(timeout) );

    setsockopt ((unsigned int) p_IdHTTP->Socket->Binding, SOL_SOCKET, SO_SNDTIMEO,
                 (char *)&timeout, sizeof(timeout));

}

0

There are 0 answers