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;
}