通过C

时间:2015-11-30 13:57:38

标签: c sockets ftp

我正在尝试通过C中的套接字从FTP服务器下载文件。客户端已经登录。控制套接字正常。数据套接字没问题。但它仍然无法下载文件(使用RETR命令)。我从FTP服务器收到以下错误消息:

  

服务器回复:421本地资源故障:打开数据端口失败。

我不明白这个消息。有没有人有任何想法?

int main(int argc , char *argv[])
{

  //Initial setting
  int CONTROL_CONNECTION_PORT = 21;
  int DATA_CONNECTION_PORT = 20;
  strcpy(MY_HOST_IP,"xxx.xxx.xxx.xxx");
  strcpy(MY_DATA_PORT, ",34,184");//8888
  strcpy(FTP_IP, argv[1]);
  strcpy(FTP_ID, "ooxx");
  strcpy(FTP_PWD, "xxoo");
  strcpy(FILE_NAME, argv[2]);


  sprintf(Buff, "WSASTARTUP = %d", ReturnValue);
  output(Buff);

  // Connect to the server(CONTROL SOCKET)
  SOCKET ControlSocket = ConnectFTP(FTP_IP, CONTROL_CONNECTION_PORT);//CONNECT IP:xxx.xxx.xxx.xxx  PORT: 21 == 0
  // Server reply: FTP server ready.
  receiving(ControlSocket);


  // Send our username to the ftp-server.
  sprintf(Buff,"USER %s",FTP_ID);
  sending(ControlSocket, Buff);
  // Server reply : 331 Password required for User ooxx.
  receiving(ControlSocket);


  // Send our password to the ftp-server.
  sprintf(Buff,"PASS %s",FTP_PWD);
  sending(ControlSocket, Buff);
  // Server reply : 230 User ooxx logged in.
  receiving(ControlSocket);


  // In Active mode (DATA PROT SETTING)
  sprintf(Buff, "PORT %s%s",MY_HOST_IP,MY_DATA_PORT);
  sending(ControlSocket, Buff);
  // Server reply : 200 PORT command successful.
  receiving(ControlSocket);


  // Connect to the server(DATA SOCKET)
  SOCKET DataSocket = ConnectFTP(FTP_IP, DATA_CONNECTION_PORT);//CONNECT IP:xxx.xxx.xxx.xxx  PORT: 20 == 0


  // Get a file from server
  sprintf(Buff,"RETR %s", FILE_NAME);
  sending(ControlSocket, Buff);
  // Server reply : 150 Opening BINARY mode data connection for FileA.
  receiving(ControlSocket);
  // "Server reply : 421 Local resource failure: open data port failed."
  receiving(ControlSocket);



  // ---RECEIVING FILE FROM THE DATA SOCKET---
  receiving(DataSocket);//?????

  // Server reply.
  receiving(ControlSocket);//??????


  //closed socket
  closesocket(ControlSocket);
  closesocket(DataSocket);

  WSACleanup();

  return 0;
}

0 个答案:

没有答案