Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 386, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

WinInet编程中连接超时的解决方法

在WinInet编程中,SetOption函数用来设置超时,可经过测试,无效。在MSDN文档中明确指出“InternetSetOption Does Not Set Timeout Values”,微软提出的解决方法是用线程来实现,示例代码如下:
  1 #include <windows.h>
  2    #include <wininet.h>
  3    #include <iostream.h>
  4 
  5    DWORD WINAPI WorkerFunction( LPVOID ); 
  6    HINTERNET g_hOpen, g_hConnect;
  7 
  8    typedef struct 
  9    {
 10        CHAR* pHost;
 11        CHAR* pUser;
 12        CHAR* pPass;
 13    } PARM;
 14 
 15    void main()
 16    {
 17        CHAR    szHost[] = "localhost";
 18        CHAR    szUser[] = "JoeB";
 19        CHAR    szPass[] = "test";
 20        CHAR    szLocalFile[] = "localfile";
 21        CHAR    szRemoteFile[] = "remotefile";
 22        DWORD   dwExitCode;
 23        DWORD   dwTimeout;
 24        PARM    threadParm;
 25 
 26        g_hOpen = 0;
 27        if ( !( g_hOpen = InternetOpen ( "FTP sample"
 28                                         LOCAL_INTERNET_ACCESS, 
 29                                         NULL, 
 30                                         0
 31                                         0 ) ) )
 32        {         
 33            cerr << "Error on InternetOpen: " << GetLastError() << endl;
 34            return ;
 35        }
 36 
 37        // Create a worker thread 
 38        HANDLE   hThread; 
 39        DWORD    dwThreadID;
 40        threadParm.pHost = szHost;
 41        threadParm.pUser = szUser;
 42        threadParm.pPass = szPass;
 43 
 44        hThread = CreateThread(
 45                      NULL,            // Pointer to thread security attributes 
 46                      0,               // Initial thread stack size, in bytes 
 47                      WorkerFunction,  // Pointer to thread function 
 48                      &threadParm,     // The argument for the new thread
 49                      0,               // Creation flags 
 50                      &dwThreadID      // Pointer to returned thread identifier 
 51                  );    
 52 
 53        // Wait for the call to InternetConnect in worker function to complete
 54        dwTimeout = 5000// in milliseconds
 55        if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
 56        {
 57            cout << "Can not connect to server in " 
 58                 << dwTimeout << " milliseconds" << endl;
 59            if ( g_hOpen )
 60    InternetCloseHandle ( g_hOpen );
 61            // Wait until the worker thread exits
 62            WaitForSingleObject ( hThread, INFINITE );
 63            cout << "Thread has exited" << endl;
 64            return ;
 65        }
 66     
 67        // The state of the specified object (thread) is signaled
 68        dwExitCode = 0;
 69        if ( !GetExitCodeThread( hThread, &dwExitCode ) )
 70        {
 71            cerr << "Error on GetExitCodeThread: " << GetLastError() << endl;
 72            return ;
 73        }
 74 
 75        CloseHandle (hThread);
 76        if ( dwExitCode )
 77        // Worker function failed
 78           return ;
 79     
 80        if ( !FtpGetFile ( g_hConnect, 
 81                           szRemoteFile,
 82                           szLocalFile,
 83                           FALSE,INTERNET_FLAG_RELOAD, 
 84                           FTP_TRANSFER_TYPE_ASCII,
 85                           0 ) )
 86        {
 87            cerr << "Error on FtpGetFile: " << GetLastError() << endl;
 88            return ;
 89        }
 90 
 91        if ( g_hConnect )
 92            InternetCloseHandle( g_hConnect );
 93        if ( g_hOpen )
 94            InternetCloseHandle( g_hOpen );
 95 
 96        return ;
 97    }
 98 
 99    /////////////////// WorkerFunction /////////////////////
100    DWORD WINAPI 
101    WorkerFunction(
102        IN LPVOID vThreadParm
103    )
104    /*
105    Purpose:
106        Call InternetConnect to establish a FTP session  
107    Arguments:
108        vThreadParm - points to PARM passed to thread
109    Returns:
110        returns 0  
111    */ 
112    {
113        PARM* pThreadParm;
114        // Initialize local pointer to void pointer passed to thread
115        pThreadParm = (PARM*)vThreadParm;
116        g_hConnect = 0;
117     
118        if ( !( g_hConnect = InternetConnect (
119                                 g_hOpen, 
120                                 pThreadParm->pHost,
121                                 INTERNET_INVALID_PORT_NUMBER,
122                                 pThreadParm->pUser,
123 pThreadParm->pPass,
124                                 INTERNET_SERVICE_FTP, 
125                                 0,
126                                 0 ) ) )
127        {
128            cerr << "Error on InternetConnnect: " << GetLastError() << endl;
129            return 1// failure
130        }
131        
132        return 0;  // success
133    }
134 

一个老外写的同步类
W3C  w3csample

posted on 2009-05-17 16:17 Benjamin 阅读(5840) 评论(0)  编辑 收藏 引用 所属分类: VC


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理