0

I need to help with my ftp client application. I successfuly connect to server with usename and password and send PASV message. I recived IP and port, but i do not know what to do next. I know that I must connect to IP and port, but I do not know how.
Can somebody give me example of code how to connect and recive message from server?

Allda
  • 128
  • 1
  • 9

1 Answers1

0

You already have the code which successfully connected to port 21 on the server, the same code will also work to connect to the IP address and port number that was returned in the PASV response.

BUT

You do not want to make this second connection until it is time to transfer some data. The first connection you established is for commands and responses to commands. The connection you make to the port returned by PASV is for the transfer of data following a command that actually transfers a file, for example RETR or LIST.

little_birdie
  • 5,600
  • 3
  • 23
  • 28
  • 1
    I already send LIST command and then make new connection, but everytime i have got connect error `bzero(&sout,sizeof(sout)); sout.sin_family = PF_INET; cout.sin_port = htons(newPort); sout.sin_addr.s_addr = inet_addr(newIP.c_str()); if((connect(mysocket,(struct sockaddr *)&sout,sizeof(sin))) < 0){ cerr << "Connection Error" << endl; return -1; } ` – Allda Feb 26 '14 at 23:41
  • Instead of just printing "Connection Error", use something like: cerr << "connect: " << strerror(errno) << "\n"; Then you will be able to see why the connect didn't work. – little_birdie Feb 26 '14 at 23:48
  • Ok then, there is something wrong with newIP. You need to print that out and see if it looks like a valid IP address (and, I think it has to be IPv4.. since you are using the old inet_addr() function.. but I'm not 100% sure on that one)... The problem here is probably in the code where you are parsing the server response to your PASV command. But I can't see that code so can't help you there. But I think you know how to solve this problem now. Print out the value of newIP and if it's wrong, work backward from there until you find out where you are doing it wrong. – little_birdie Feb 27 '14 at 00:05
  • You are likely not parsing the `PASV` response correctly. It **does not** have a standardized format, and there are several different formats used by various FTP servers, so you have to make sure you handle them all (`EPSV`, on the other hand, has a standardized format). You might also consider ignoring the `PASV` IP an just use the same IP that your command socket is connected to. – Remy Lebeau Feb 27 '14 at 01:24
  • Thanks guys. I call getIP from recived buffer on bad response. – Allda Feb 27 '14 at 09:18
  • Great, glad you got it working. Please 'accept' the answer! Thanks. – little_birdie Feb 27 '14 at 20:57