一个ftp文件传输程序,用internetconnect()连接ftp站点,如果连接的ip地址不提供ftp服务,则用internetconnect()连接速度超慢.请教如何设置超时时间,我用internetsetoption(hopen,INTERNET_OPTION_CONNECT_TIMEOUT,@tmpTimeOut,sizeof(tmpTimeOut));
为什么没有效果!
程序段如下:var
  tmpTimeOut:dword;
begin
  hopen:=internetopen(pchar('hello'),0,NIL,NIL,0);
  tmpTimeOut:=10;
  aa:=internetsetoption(hopen,INTERNET_OPTION_CONNECT_TIMEOUT,@tmpTimeOut,sizeof(tmpTimeOut));
          hconnection:=internetconnect(hopen,pchar(172.18.26.235),21,'Anonymous','User@',internet_service_ftp,0,255);

解决方案 »

  1.   

    msdn 上说了,ftp设超时没有用。
    上面还有个示例程序,介绍如何用两个线程的方法自己控制超时。
      

  2.   

    msdn 上面的解释,没有说ftp设超时没有用。高人帮忙,问题依旧
    INTERNET_OPTION_CONNECT_TIMEOUT 
    Sets or retrieves an unsigned long integer value that contains the time-out value to use for Internet connection requests. Units are in milliseconds. If a connection request takes longer than this time-out value, the request is canceled. When attempting to connect to multiple IP addresses for a single host (a multihome host), the timeout limit is cumulative for all of the IP addresses. This option can be used on any HINTERNET handle, including a NULL handle. It is used by InternetQueryOption and InternetSetOption.
      

  3.   

    看来不写清楚真不行啊:http://support.microsoft.com/default.aspx?scid=kb;en-us;176420
    http://support.microsoft.com/kb/224318/EN-US/
      

  4.   

    谁给把这段代码翻译成delphi先,我底子差
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
      
       #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
       }
      

  5.   


    谁给把这段代码翻译成delphi先,我底子差
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
      
       #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
       }