FTP server recieve files without content

Hi everyone
I am trying to send a text , picture and video separately by using an Arduino Mega 2560. the server is my PC with a Filezilla server. using the following code which I found in the Arduino site I am trying to download or upload files.
But in the first step when I tried to send a text file with 1 kB length, I just could to see an empty text file in server.
Would you explain me how can I solve this problem?
sincerely

#include <SD.h>
#include <SPI.h>
#include <WiFi.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE

// this must be unique
byte mac[] = {0x78, 0xC4, 0xE, 0x01, 0x9C, 0xE3};

// change to your network settings
IPAddress ip ###########
IPAddress  gateway#########
IPAddress subnet############
char ssid[]= "########";
char pass[]= "########";
// change to your server
IPAddress server(########);

WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "TEXT3.TXT";
void setup()
{
  Serial.begin(9600);

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }

  WiFi.begin(ssid,pass); 
  digitalWrite(10,HIGH);
  delay(2000);
  Serial.println(F("Ready. Press f or r"));
}
void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'f')
  {
    if(doFTP()) Serial.println(F("FTP OK"));
    else Serial.println(F("FTP FAIL"));
  }
  if(inChar == 'r')
  {
    readSD();    
  }
}

File fh;

byte doFTP()
{
#ifdef FTPWRITE
 fh = SD.open(fileName,FILE_READ);
  Serial.println(fh);
  //fh = SD.open("Text3.txt",FILE_READ);
#else
  SD.remove(fileName);
  fh = SD.open(fileName,FILE_WRITE);
#endif
  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return 0;    
  }
#ifndef FTPWRITE  
  if(!fh.seek(0))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;    
  }
#endif

  Serial.println(F("SD opened"));

  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  } 
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  client.println(F("USER #####"));

  if(!eRcv()) return 0;

  client.println(F("PASS #######"));

  if(!eRcv()) return 0;

  client.println(F("SYST"));

  if(!eRcv()) return 0;

  client.println(F("PASV"));

  if(!eRcv()) return 0;

  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(F("Bad PASV Answer"));    
    }
  }

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

  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);
  
  if (dclient.connect(server,hiPort)) {
    Serial.println(F("Data connected"));
  } 
  else {
    Serial.println(F("Data connection failed"));
    client.stop();
    fh.close();
    return 0;
  }
#ifdef FTPWRITE 
  client.print(F("STOR "));
  client.println(fileName);
#else
  client.print(F("RETR "));
  client.println(fileName);
#endif

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

#ifdef FTPWRITE
  Serial.println(F("Writing"));
//_______________________________________________________________________________________________________________________________________________________________________

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);      
      Serial.write(c); 
    }
  }
#endif

  dclient.stop();
  Serial.println(F("Data disconnected"));

  if(!eRcv()) return 0;

  client.println(F("QUIT"));

  if(!eRcv()) return 0;

  client.stop();
  Serial.println(F("Command disconnected"));

  fh.close();
  Serial.println(F("SD closed"));
  return 1;
}
byte eRcv()
{
  byte respCode;
  byte thisByte;

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

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

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.println(F("QUIT"));

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

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

  client.stop();
  Serial.println(F("Command disconnected"));
  fh.close();
  Serial.println(F("SD closed"));
}

void readSD()
{
  fh = SD.open(fileName,FILE_READ);

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return;    
  }

  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

I was fighting with this yesterday, if I turned the Firewall off on my WinXP PC (I am using the WinXP FTP Server), this code worked for me.

Good Luck...

Insure the user you are logging in with has write permissions in that directory. I use Linux with that code and it works fine. It should. I wrote that sketch. :slight_smile:

I have Win 8.1 64x on my system.
First of all I tried to turn firewall off But nothing happened then I checked fillzilla permission I got the same result .
Now I am trying to debug the code by putting some Serial.print .
I have problem in the following part

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;
    Serial.println(clientCount);

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }

Expecting result is 64 counting but I just have 42 counting .
when I change 64 to 32 I have the same counting.

Insure you have the last line here. It sends any amount smaller than 64.

   while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

// this sends any bytes remaining in the clientBuf
  if(clientCount > 0) dclient.write(clientBuf,clientCount);

Yes I have it.
This is the result

Ready. Press f or r
1
SD opened
Command connected
220-FileZilla Server version 0.9.43 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit FileZilla® download | SourceForge.net
331 Password required for ##
230 Logged on
215 UNIX emulated by FileZilla
227 Entering Passive Mode (#,#,#,#,#,#,#)
Data port: 53413
Data connected
150 Opening data channel for file upload to server of "/TEXT3.TXT"
Writing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Data disconnected

It appears it is working ok. If the file is still empty on the server, insure the file permissions are set correctly.

If you tried with Filezilla and got the same empty file, then it is probably a permission challenge on the server.

I changed the sharing part in properties of server folder.but again it is empty

Are you using the Windows FTP server? Maybe is a setting in it. Read this and insure you have your FTP server settings changed to allow writes.
http://support.microsoft.com/kb/323384

I tried to create an FTP server windows but in last step could't to be connect to the server so then Because of made doubt on access point and server,first I checked access point configuration but it was OK,then tried to send a file from another PC as client to my PC as server finally could to receive file,by the way now I think my server has not problem !!.
I formatted the SD card,the counted number increased from 42 to 64 and so on..
But again I have problem in receiving file and it is empty.
I am trying with WiFi but your original code is based on Ethernet,This would not be a problem?

I see. You are using a wifi shield. I have one here. Give me a chance to play with it. It may be a couple hours before I finish the project I have going. Then I will experiment.

Sorry. The wifi shield doesn't work with the FTP client. :frowning:

It does ok until the data connection is closed, but after that the wifi shield command connection (socket) never receives the status of the send, and I'm not sure the FTP server gets the "QUIT" message. That is what causes the server to write the data and close the file.

No problem.I will try to solve my problem in the other way.
Thanks for your consideration and time.
Regards