I am on a very similar project, a minimal ftp server for Due and WIZ820io, for the same reason: I am realizing a mp3 player (see my web page: LaRocola ) and don't want to remove the microSD card from my reader each time I want to update the files.
I had the same problem: function server.available() only return true if, after having connected, the client send at least one character.
And RFC 959 is clear, the sequence for initializing a ftp connection must be in this order:
1- client connect
2- server detect connection
3- server send code 220
4- client send his first command (normally USER)
For me, the fault is of the Ethernet library
How I solve it:
First, try this code (change values of mac and IPAddress variables):
#include <Streaming.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0x00, 0x00 };
IPAddress ip( 192, 168, 1, 100 );
EthernetServer ftpServer( 21 );
EthernetServer dataServer( 20 );
EthernetClient client;
void setup()
{
Serial.begin(9600);
Serial << "Initiating Ftp Server on Due" << endl;
// Initialize the network
//Ethernet.begin( mac, ip, gateway, gateway, subnet );
Ethernet.begin( mac, ip );
// Tells the ftp server to begin listening for incoming connection
ftpServer.begin();
delay( 2000 );
Serial << "Ftp server waiting for connection on port 21." << endl;
}
void loop()
{
// wait for a client to connect
client = ftpServer.available();
if( client == true )
{
Serial << "Client connected!" << endl;
client << "220 Welcome to FTP for Arduino\r\n";
// echo any character send to server for 20 seconds
int32_t millisEndConnection = millis() + 20 * 1000;
while( (long) ( millisEndConnection - millis() ) > 0 )
{
if( client.available() )
{
char c = client.read();
client << c;
Serial << c;
}
}
Serial << "Timeout. Disconnecting client" << endl;
client.stop();
delay( 1000 );
Serial << "Ftp server waiting for connection on port 21." << endl;
}
}
We know it does not work.
Now, close the ide and open the file arduino-1.5.2/hardware/arduino/sam/libraries/Ethernet/EthernetServer.cpp in you editor.
Look at function EthernetServer::available() (on line 51):
EthernetClient EthernetServer::available()
{
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED ||
client.status() == SnSR::CLOSE_WAIT)) {
if (client.available()) {
// XXX: don't always pick the lowest numbered socket.
return client;
}
}
}
return EthernetClient(MAX_SOCK_NUM);
}
The first if statement check for connection on socket sock
The second if statement check for data available for reading on that socket. We must remove it or comment the lignes 60 and 63:
EthernetClient EthernetServer::available()
{
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED ||
client.status() == SnSR::CLOSE_WAIT)) {
// if (client.available()) {
// XXX: don't always pick the lowest numbered socket.
return client;
// }
}
}
return EthernetClient(MAX_SOCK_NUM);
}
Rerun the previous sketch and try to connect with your ftp client. It works for me with FileZila
Of course, modifying the function EthernetServer::available() is not a clean solution. Surely, some programs there where working fine before would fail...
So, instead of modifying that function I add a new one, EthernetServer::connected() :
EthernetClient EthernetServer::connected()
{
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED ||
client.status() == SnSR::CLOSE_WAIT))
return client;
}
return EthernetClient(MAX_SOCK_NUM);
}
I repeat: I make this test with the Arduino Due, but I think this is the same problem with others Arduinos.
May be someone can probe it on Mega2560 and/or with others ftp clients.
Remember that libraries for AVR microcontrollers are in arduino-1.x.x/hardware/arduino/avr
Hope this would help