为什么总是报“无法解析服务器的名臣和地址”

zhu214002014 2010-02-02 07:35:19
写的一个程序,要求实现的是它可以自动去指定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;
}
在运行时,报出错误“无法解析服务器的名称和地址”

求教
...全文
269 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhu214002014 2010-02-02
  • 打赏
  • 举报
回复
问题解决了,是pcServer问题,它只可以写地址,比如"127.0.0.1"
加上别的就会出错,现在问题是我连接上了,但是我怎么下载东西呢?
从代码上看应该可以下载程序了!
songsu 2010-02-02
  • 打赏
  • 举报
回复
改一下 bPassive 方式试
zhu214002014 2010-02-02
  • 打赏
  • 举报
回复
我在测试的时候是用的正确的ip,端口是21
我在用一个ftp工具连接的时候就完全没有问题
wuhuwy 2010-02-02
  • 打赏
  • 举报
回复
可能是目标主机的Ip或端口出错了

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧