为什么总是报“无法解析服务器的名臣和地址”
写的一个程序,要求实现的是它可以自动去指定ftp服务器(已经在程序里面写死)上下载一段指定的程序到本机。
并不是要实现一个客户端-服务端机制。
实现代码如下:
// FTPdownload.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <afxinet.h>
bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
bool state;
bool TURE=1;
const char pcLocalFile[100]="d:";
const char pcServer[50]="ftp://11.10.112.0";//ip地址为随意填写的
const char pcUserName[6]="test";//用户名
const char pcPassword[10]="123123";//密码
// printf("请输入下到本机的路径:\n");
// scanf("%s",pcLocalFile);
printf("正在下载,请稍后。。。\n");
state=FtpDownload(pcLocalFile,"360.exe",pcServer,pcUserName,pcPassword,21,TRUE);
if(state==TURE)
{
printf("下载完毕!\n");
}
else
{
printf("下载失败,请从新运行本程序!\n");
}
return nRetCode;
}
bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */)
{
CInternetSession * pInetSession = NULL;
CFtpConnection * pFtpConnection = NULL;
bool bRet = false;
if (pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS))
{
pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pInetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);
TRACE("[FTP Addr]\t\t%s\n", pcServer);
TRACE("[FTP User Name]\t%s\n", pcUserName);
TRACE("[FTP Password]\t%s\n", pcPassword);
try
{
pFtpConnection = pInetSession->GetFtpConnection(pcServer, pcUserName, pcPassword, nPort, bPassive);//我在这里做了断点,发现这里运行总是报错
}
catch (CInternetException *pEx)
{
pFtpConnection = NULL;
pEx->ReportError(MB_ICONEXCLAMATION);
pEx->Delete();
}
if (pFtpConnection)
{
TRACE("[Download Src]\t\t%s\n", pcRemoteFile);
TRACE("[Download Dst]\t\t%s\n", pcLocalFile);
bRet = pFtpConnection->GetFile(pcRemoteFile, pcLocalFile) ? true : false;
}
}
if (pInetSession)
{
pInetSession->Close();
delete pInetSession;
}
if (pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
}
return bRet;
}
在运行时,报出错误“无法解析服务器的名称和地址”
求教