一个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);
为什么没有效果!
程序段如下: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);
解决方案 »
- 请问scrollbar来控制控件移动的问题!
- 在delphi中如何格式化U盘成fat16格式
- 请问在局域网中怎样判断另台机子上的某个程序正在运行?
- 怎么才能知道数据控件的SQL语句执行的情况呢??我的分全送光了对不起大家了|!
- error loading midas.dll? 怎么会这样
- 急问一个关于打印多页fastreport报表.详细见内!( 各位帮忙啊!!!!);
- 桌面宠物类的游戏Delphi可不可做?请具体说一下
- http://chinaour.com/?weiowen大家散分啦
- 怎么使在编辑stringgrid时能够多行显示?
- 用ADODataset1查询的结果<1时就报错!
- 关于mxOutlookBarPro的问题!??急用!
- 请问我如何取的网页中的FLASH
上面还有个示例程序,介绍如何用两个线程的方法自己控制超时。
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.
http://support.microsoft.com/kb/224318/EN-US/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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
}
谁给把这段代码翻译成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
}