Programming/WINAPI
[펌] InternetSetOption - timeout 설정 버그 해결방법
smok95
2009. 1. 5. 19:12
반응형
InternetSetOption 함수를 이용하여 timeout 시간을 설정해도 적용이 되지 않는 버그가 있습니다.
돌아다니는 이야기로는 Internet Explorer 6.0 버전 이상이면 문제가 없다는데 제가 테스트한 환경 ( Windows 2003서버, IE6 )
에서는 문제가 발생하는 것으로 보아 문제가 완전히 해결된것이 아닌가 봅니다.
아래는 해당 버그 대안으로 쓰레드를 이용하여 timeout을 설정하는 방법을 설명한 내용입니다.
------------------------------------------------------------------------------------------------------------------
출처 : http://support.microsoft.com/kb/224318
돌아다니는 이야기로는 Internet Explorer 6.0 버전 이상이면 문제가 없다는데 제가 테스트한 환경 ( Windows 2003서버, IE6 )
에서는 문제가 발생하는 것으로 보아 문제가 완전히 해결된것이 아닌가 봅니다.
아래는 해당 버그 대안으로 쓰레드를 이용하여 timeout을 설정하는 방법을 설명한 내용입니다.
------------------------------------------------------------------------------------------------------------------
출처 : http://support.microsoft.com/kb/224318
How To Control Connection Timeout Value by Creating Second Thread
This article was previously published under Q224318
SUMMARY
This acticle shows a workaround to the InternetSetOption API bug on setting time...
This acticle shows a workaround to the InternetSetOption API bug on setting timeout values by creating a second thread.
InternetSetOption Does Not Set Timeout Values
MORE INFORMATION
The following sample code controls how long to wait while connecting to the FTP...
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.
#include <windows.h> #include <wininet.h> #include <iostream.h> DWORD WINAPI WorkerFunction( LPVOID ); HINTERNET g_hOpen, g_hConnect; typedef struct { CHAR* pHost; CHAR* pUser; CHAR* pPass; } PARM; void main() { 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 ) ) ) { cerr << "Error on InternetOpen: " << GetLastError() << endl; return ; } // 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 ) { 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 ; } // The state of the specified object (thread) is signaled dwExitCode = 0; if ( !GetExitCodeThread( hThread, &dwExitCode ) ) { cerr << "Error on GetExitCodeThread: " << GetLastError() << endl; return ; } CloseHandle (hThread); if ( dwExitCode ) // Worker function failed return ; if ( !FtpGetFile ( g_hConnect, szRemoteFile, szLocalFile, FALSE,INTERNET_FLAG_RELOAD, FTP_TRANSFER_TYPE_ASCII, 0 ) ) { cerr << "Error on FtpGetFile: " << GetLastError() << endl; return ; } if ( g_hConnect ) InternetCloseHandle( g_hConnect ); if ( g_hOpen ) InternetCloseHandle( g_hOpen ); return ; } /////////////////// 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 */ { 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 ) ) ) { cerr << "Error on InternetConnnect: " << GetLastError() << endl; return 1; // failure } return 0; // success }
반응형