Can't connect to FTP server Via Wifi Shield

I keep trying to connect to my FTP server using the Wifi shield. I input the correct username and password but it keeps failing.
Here’s the code i’m using

/*
 
 This sketch connects to a Wifi network. 
 Then it prints the  MAC address of the Wifi shield,
 the IP address obtained, and other network details.
 Then transfer file from SD card on ftp server (if press f)

 Circuit:
 * WiFi shield attached
 
 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe -- WiFi part
 October 2012 by SurferTim -- ftp part
 October 2013 by Pashkot -- put the parts together (Arduino IDE 1.0.5, Arduino UNO R3)

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

char ssid[] = "FIU_WiFi";     //  your network SSID (name) 
//char pass[] = "mypassowrd";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

// change to your server
IPAddress server( xx, xx, xxx, xxx );


WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
//char fileName[13] = "PIC_1204.JPG";
char fileName[13] = "test.txt";

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  //while (!Serial) {
    //; // wait for serial port to connect. Needed for Leonardo only
  //}
  
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
 // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);

    // Connect to WPA/WPA2 network:    
    status = WiFi.begin(ssid);

    // wait 10 seconds for connection:
    delay(10000);

    // you're connected now, so print out the data:
    Serial.print("You're connected to the network ");
    printCurrentNet();
    printWifiData();
    Serial.print("FTP server:");
    Serial.println(server);
    Serial.println(F("Ready. Press f or r"));
  }
}

void loop() {
  // check the network connection once every 10 seconds:
  //delay(10000);
  //printCurrentNet();
  
  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);
#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 xxxxx"));


  if(!eRcv()) return 0;

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


  if(!eRcv()) return 0;

  client.println(F("SYST\r\n"));

  if(!eRcv()) return 0;

  client.println(F("PASV\r\n"));

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


void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
  Serial.println(ip);
    
  // print your MAC address:
  byte mac[6];  
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.println(mac[0],HEX);
 
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);    
  Serial.print("BSSID: ");
  Serial.print(bssid[5],HEX);
  Serial.print(":");
  Serial.print(bssid[4],HEX);
  Serial.print(":");
  Serial.print(bssid[3],HEX);
  Serial.print(":");
  Serial.print(bssid[2],HEX);
  Serial.print(":");
  Serial.print(bssid[1],HEX);
  Serial.print(":");
  Serial.println(bssid[0],HEX);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption,HEX);
  Serial.println();
}

This is what message I keep getting

Attempting to connect to WPA SSID: FIU_WiFi
You're connected to the network SSID: FIU_WiFi
BSSID: 6C:9C:ED:87:FA:11
signal strength (RSSI):-68
Encryption Type:7

IP Address: 10.110.0.194
MAC address: 7A:C4:E:94:EB:87
FTP server:xx.xx.xxx.xxx
Ready. Press f or r
SD opened
Command connection failed
FTP FAIL

Is this an Arduino WiFi shield? If so, have you upgraded the firmware?

But it won't matter if the FTP command connection fails. I have already determined the data connection will fail. The WiFi shield is not capable of having two sockets open simultaneously. :(

Thanks for responding. I have updated my WiFi shield firmware but I dont think i quite understand the whole socket thing. Are you saying that there is no way to connect to an FTP server via my Wifi shield? Also if this is true, is there any other way I can wirelessly upload an image from the SD card to my computer via a Wifi shield?

Ramaru: Is there any other way I can wirelessly upload an image from the SD card to my computer via a Wifi shield?

Use HTTP instead.

What do you mean by use "HTTP"?

SurferTim:
Is this an Arduino WiFi shield? If so, have you upgraded the firmware?

But it won’t matter if the FTP command connection fails. I have already determined the data connection will fail. The WiFi shield is not capable of having two sockets open simultaneously. :frowning:

Wow, that sucks! I didn’t know FTP needs two sockets. What other protocols use more than one socket?

OP,

In response to your PM, I am not using FTP. I have a server set up to receive HTTP POST method. I POST data to the server as a stream of numbers, not as a file, which if what you are doing.

To help you solve your problem, I propose the following:

See if you HAVE to use wifi shield. It's expensive, and not very sophisticates to handle some secure stuff or as TIm mentioned the socket issue. If you have to use wifi, see if ethernet shield can handle two sockets. Then use a wifi ethernet bridge to get wifi connectivity. This combination is probably cheaper than wifi shield.

FTP is the only protocol I use that does require two sockets. The ethernet shield doesn't have a problem with FTP. It works fine using two sockets simultaneously.

Ramaru: What do you mean by use "HTTP"?

I mean use the HTTP protocol instead of the FTP protocol. You can use HTTP to upload a file, and it doesn't require multiple sockets.

Thanks for the response guys and yes it has to be either a wifishield or a bluetooth shield. I figured the wifi shield would be a lot easier to use. I'm trying to use the wifi shield as a web server but so far no luck.