070102 InternetOpen thread timerout
如何通过创建另一个线程控制连接超时值
How To Control Connection Timeout Value by Creating Second Thread
SUMMARY
This acticle shows a workaround to the InternetSetOption API bug on setting timeout values by creating a second thread. For information about the bug, please see the following article in the Microsoft Knowledge Base:
176420 (http://support.microsoft.com/kb/176420/EN-US/) InternetSetOption Does Not Set Timeout Values
MORE INFORMATION
The following sample code controls how long to wait while connecting to the FTP server in WinInet. It creates a worker thread to call the blocking WinInet APIs. If the connection takes more time than the specified timeout value, the original thread will call InternetCloseHandle to release the blocking WinInet function. For HTTP communications the same idea applies. In the case of HTTP, the actual connection occurs in the call to HttpSendRequest.
要对设置超值 InternetSetOption API 错误由创建另一个线程此 acticle 显示解决。 有关错误, 请参见下列文章 Microsoft 知识库文章:
176420 (http://support.microsoft.com/kb/176420/EN-US/) InternetSetOption 不设置超时值
要连接到 FTP 服务器 WinInet 中时等待时间以下示例代码控件。 创建辅助线程以调用阻塞 WinInet API。 如果该连接所花的时间比指定超值, 原始线程将调用 InternetCloseHandle 来释放阻塞 WinInet 函数。 对于 HTTP 通讯应用同一思路。 万一有 HTTP, 实际连接发生在 HttpSendRequest 调用。 #include <windows.h>
#include <wininet.h>
#include <iostream.h>
DWORD WINAPI WorkerFunction( LPVOID );
HINTERNET g_hOpen, g_hConnect;
typedef struct
&leftsign;
CHAR* pHost;
CHAR* pUser;
CHAR* pPass;
&rightsign; PARM;
void main()
&leftsign;
CHAR szHost[] = "localhost";
CHAR szUser[] = "JoeB";
CHAR szPass[] = "test";
CHAR szLocalFile[] = "localfile";
CHAR szRemoteFile[] = "remotefile";
DWORD dwExitCode;
DWORD dwTimeout;
PARM threadParm;
g_hOpen = 0;
if ( !( g_hOpen = InternetOpen ( "FTP sample",
LOCAL_INTERNET_ACCESS,
NULL,
0,
0 ) ) )
&leftsign;
cerr << "Error on InternetOpen: " << GetLastError() << endl;
return ;
&rightsign;
// Create a worker thread
HANDLE hThread;
DWORD dwThreadID;
threadParm.pHost = szHost;
threadParm.pUser = szUser;
threadParm.pPass = szPass;
hThread = CreateThread(
NULL, // Pointer to thread security attributes
0, // Initial thread stack size, in bytes
WorkerFunction, // Pointer to thread function
&threadParm, // The argument for the new thread
0, // Creation flags
&dwThreadID // Pointer to returned thread identifier
);
// Wait for the call to InternetConnect in worker function to complete
dwTimeout = 5000; // in milliseconds
if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
&leftsign;
cout << "Can not connect to server in "
<< dwTimeout << " milliseconds" << endl;
if ( g_hOpen )
InternetCloseHandle ( g_hOpen );
// Wait until the worker thread exits
WaitForSingleObject ( hThread, INFINITE );
cout << "Thread has exited" << endl;
return ;
&rightsign;
// The state of the specified object (thread) is signaled
dwExitCode = 0;
if ( !GetExitCodeThread( hThread, &dwExitCode ) )
&leftsign;
cerr << "Error on GetExitCodeThread: " << GetLastError() << endl;
return ;
&rightsign;
CloseHandle (hThread);
if ( dwExitCode )
// Worker function failed
return ;
if ( !FtpGetFile ( g_hConnect,
szRemoteFile,
szLocalFile,
FALSE,INTERNET_FLAG_RELOAD,
FTP_TRANSFER_TYPE_ASCII,
0 ) )
&leftsign;
cerr << "Error on FtpGetFile: " << GetLastError() << endl;
return ;
&rightsign;
if ( g_hConnect )
InternetCloseHandle( g_hConnect );
if ( g_hOpen )
InternetCloseHandle( g_hOpen );
return ;
&rightsign;
/////////////////// WorkerFunction //////////////////////
DWORD WINAPI
WorkerFunction(
IN LPVOID vThreadParm
)
/*
Purpose:
Call InternetConnect to establish a FTP session
Arguments:
vThreadParm – points to PARM passed to thread
Returns:
returns 0
*/
&leftsign;
PARM* pThreadParm;
// Initialize local pointer to void pointer passed to thread
pThreadParm = (PARM*)vThreadParm;
g_hConnect = 0;
if ( !( g_hConnect = InternetConnect (
g_hOpen,
pThreadParm->pHost,
INTERNET_INVALID_PORT_NUMBER,
pThreadParm->pUser,
pThreadParm->pPass,
INTERNET_SERVICE_FTP,
0,
0 ) ) )
&leftsign;
cerr << "Error on InternetConnnect: " << GetLastError() << endl;
return 1; // failure
&rightsign;
return 0; // success
&rightsign;
历史博文
- 20081007 c# ip wmi - 2009
- 20070912 minigui 触摸屏 tslib 资料 - 2008
- 0220 Eclipse cdt mingw IDE - 2006
- Windows下的CVS版本控制软件使用-CVS服务器端CVSNT - 2005