Data download FTP

Hello everybody,

I would like to have some explanations on some parts of this code: http://playground.arduino.cc//Code/FTP

This part is to check the passiv connection right ?
What is: strtok(outBuf,"(,") , atoi(tStr), there is functions ??? They’re not declared anywhere…

char *tStr = strtok(outBuf,"(,"); 
int array_pasv[6];
for ( int i = 0; i < 6; i++) {
tStr = strtok(NULL,"(,");
array_pasv[i] = atoi(tStr);
if(tStr == NULL)
{
Serial.println("Bad PASV Answer"); 

}
}

And this part:
What is this function for ?
And what mean hiPort = hiPort | loPort; ?

"unsigned int hiPort,loPort;

hiPort = array_pasv[4] << 8;
loPort = array_pasv[5] & 255;

Serial.print("Data port: ");
hiPort = hiPort | loPort;
Serial.println(hiPort);"

I don’t understand how this function work:
And what is the goal of this function…

"byte eRcv()
{
byte respCode;
byte thisByte;

while(!client.available()) delay(5);

//Lit un octet du fichier sans passer au suivant
respCode = client.peek();

outCount = 0;

while(client.available())
{ 
thisByte = client.read(); 
Serial.write(thisByte);

if(outCount < 127)
{
outBuf[outCount] = thisByte;
outCount++; 
outBuf[outCount] = 0;
}
}

if(respCode = '4')
{
efail();
return 0; 
}

Thank you for reading :slight_smile:

Thank you for reading

Thank YOU for googling first.

What is: strtok(outBuf,"(,") , atoi(tStr), there is functions ??? They're not declared anywhere.

These are standard C functions.

And what mean hiPort = hiPort | loPort; ?

Those bytes are used to build the passive FTP data port (integer).

And what is the goal of this function...

That function reads the response after each send. The first character is the numeric return code. If the first character of that number is greater than 3, then the send (request) produced an error.

if(respCode >= '4')

Ok, thanks :wink:

And what is contained in “OutBuf” ?

Why do we chose 4 and 5 for the array ?

hiPort = array_pasv[4] << 8;
loPort = array_pasv[5] & 255;

did you even tried to manually download a file by telnet?

@OP: outBuf is the array that holds the 64 byte buffer for the dclient.write(). It sends one 64 byte packet rather than 64 one-byte packets.

@lesto: No, I have not tried telnet to download a file.

SurferTim:
@lesto: No, I have not tried telnet to download a file.

i'm asking at Noob99, i think he can't follow the program because he doesn't know how the FTP protocol work

@lesto: I'm telling you that that the OP is doing fine with the w5100, but not the wifi shield. If you want to post the code for the telnet connection upload/download, may I suggest doing it now, or post it in the playground.

@lesto, no I didn't try to telnet. But you're right saying I'm not an expert in FTP protocol, but I have basis...

Thanks for your answers !

you should try to download file by telnet, so you'll see the request and the response from server (with are the same used by arduino)
This will help you to understand what is doing what in the code.

lesto:
you should try to download file by telnet, so you'll see the request and the response from server (with are the same used by arduino)
This will help you to understand what is doing what in the code.

...and the Arduino sketch code to do this is where?

just an extract:

if(!eRcv()) return 0;

  client.write("USER myuser\r\n");

  if(!eRcv()) return 0;

  client.write("PASS mypassword\r\n");

  if(!eRcv()) return 0;

  client.write("SYST\r\n");

  if(!eRcv()) return 0;

  client.write("PASV\r\n");

  if(!eRcv()) return 0;

those are the same command you should use by telnet. It is not necessary to complete successfully the transition (and doing so by telnet is very much tricky), but it is useful to understand what arduino ask and what is the expected response.

Sniffing the traffic while using a ftp client give more complete data on what is going on, but it is also more difficult as normal FTP client will probably use more than one thread and UDP

edit: look what i've found as fisrt result googling "telnet ftp": "Understanding FTP using raw FTP commands and telnet"
http://www.webdigi.co.uk/blog/2009/ftp-using-raw-commands-and-telnet/