使用Embarcadero C ++ Builder 2010,我正在尝试检测连接到网络的特定Web服务器设备。
我正在通过使用TIdHTTP调用CGI函数并在一系列IP地址上分析答案来询问这些设备。
我正在使用IdHTTP-> ConnectTimeout和IdHTTP-> ReadTimeout参数,如果在超时时间之前没有连接,则抛出异常。这应该在TIdHTTP询问意外设备时发生,在这种情况下,我通过调用IdHTTP-> Free()来释放IdHTTP。
在Windows 7上运行正常,但在Windows XP上超时不适用,因此在IdHTTP.get()停止前最多可能需要10分钟。
通过删除这两个参数,应用程序在Windows XP上的工作速度提高了一些,但仍然需要与超时相比我预期的时间长。
我发现这个相关的主题没有真正的解决方案:Indy 10 + SSL = works in Windows 7, does not work on XP
这是源代码的一部分:
#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();
}
我的问题是: 为什么IdHTTP-> ConnectTimeout无法在Windows XP上运行? 您是否有任何想法替换此参数以在特定时间后释放/销毁IdHTTP?
更新
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));
}