1

I'm trying to download a file from FTP server through socket in C. The client is already logged in. Control socket is OK. Data socket is OK. But it still can't download file(with RETR Command). I get the following error message from FTP server:

Server reply : 421 Local resource failure: open data port failed.

I don't understand this message. Does anyone have any idea?

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;
}
user1766169
  • 1,932
  • 3
  • 22
  • 44
Ellison
  • 11
  • 2
  • BTW,the file on the FTP server is TEXT file. – Ellison Nov 30 '15 at 14:00
  • 1
    This looks like C code. Why did you add tag for another language (C++)? – too honest for this site Nov 30 '15 at 14:06
  • 1
    It looks like you are trying to use an active FTP as a passive FTP. http://www.slacksite.com/other/ftp.html – Šimon Tóth Nov 30 '15 at 14:10
  • SORRY I trying to remove C++ tag – Ellison Nov 30 '15 at 14:13
  • @slacksite.com/other/ftp.html – Let_Me_Be I have read the RFC959, I'm trying to write a program with Active mode. – Ellison Nov 30 '15 at 14:14
  • 1
    @Ellison Well, this isn't how active FTP works. In active FTP the server connects to you. What you are doing is passive mode. – Šimon Tóth Nov 30 '15 at 14:18
  • Hmmmm...Could you please tell me which part is wrong then I can fix this problem? – Ellison Nov 30 '15 at 14:23
  • https://stackoverflow.com/questions/1699145/what-is-the-difference-between-active-and-passive-ftp – Šimon Tóth Nov 30 '15 at 14:24
  • @Let_Me_Be Thank you !! I know the theory, but I'm not good on implementation, so i don't need to use the functions likes "bind", "listen" , "accept" right? – Ellison Nov 30 '15 at 14:32
  • How are you planning to write a socket-based program if you do not use those functions? – SergeyA Nov 30 '15 at 14:35
  • Because I found many client-side program(In active mode) example didn't use these functions, so i asked this question. – Ellison Nov 30 '15 at 14:45
  • @user1766169 Thank you for edited my post!!! It's looks better now : ) – Ellison Nov 30 '15 at 15:40
  • In Active mode, you have to open a listening socket using `bind()` and `listen()`, then send a `PORT` or `EPRT` command telling the FTP server the IP/Port where it needs to connect to reach that socket, and then use `accept()` to receive that connection. In Passive mode, you send a `PASV` or `EPSV` command to ask the server to open a listening socket and the FTP server replies telling you the IP/Port where you need to `connect()` to reach that socket. – Remy Lebeau Nov 30 '15 at 20:36
  • @RemyLebeau Thank you for your help!!! It's really a great help to me!!!!! – Ellison Dec 01 '15 at 13:38

0 Answers0