Project - Stable FTP Server

A little update of throughput with G400D: tranfert speed is 70kB/s for upload

I put all the stable FTP server code in a gadeteer project with a G400 raptor board, EthernetENC28, SDCard and USBClientDP. Version 4.3 r4. I can FTP small files to the SD card, but I cannot FTP large files most of the time. My SD card is a 16 GB Sandisk SDHC Card. I have successfully FTP a 6 MB file (Firmware.hex) once and only once using FileZilla. It took 12 minutes, but it worked that time. I didn’t change any code. FileZilla seems to timeout. I cannot get it to work anymore. I really need to be able to update firmware and application of a raptor board from an SD card remotely. FTP seems to be a good solution. Any help is appreciated.

There’s no need to update Firmware.hex to update your application. You can focus the ftp upload on the app only.

I do have a need to update firmware and application at the same time for future release for my customers. My app file is over 2 MB. I am having problem FTP a 1 MB file. I really appreciate if anybody can help me to FTP a large file.

I can remember that I had timeouts in the FTP server when transferring large files, since the timeout was based on received commands, and there is no command during file transfer. If that timeout occurs, the FTP server closes connection.
I solved this issue by resetting the timeout after each data chunk from the file stream.
But since there are active and passive mode for file streaming, I do not know if I made this modification everywhere.
Could you tell by the output window contents why the file can not transferred completely?
May be you can post the last part of the output window when the transfer was canceled.

But to be honest, I never transferred files that big.
My larger file I usually transfer is about 10 kByte, and since it was too slow for me to store it on SD card, I created the virtual in memory FTP listener.
I also have file transfers to SD card sometimes, where one of the files is about 200 kByte.

@ nsmith -
Hi,
I suspect that the problem is not the „Stable ftp server" and not the ftp client but the process of writing the data to the SD Card. I had this experience when I transferred files with a size of up to 3 Mbyte via bluetooth. You can find it out by searching the code segment where the data chunks are written on the SD-Card in the „Stable ftp server" and instead of writing the data on the SD-Card simply neglect them. I believe that then your „file transfer" comes to an end with no blocking or exceptions.

Edit: Just had a look into the code. It seems not so easy to find the part where the data are written on the SD-Card. At least I didn’t find it yet.

I found out something interesting. If I use FileZilla, I can write a 6 MB file to an SD card once, then I cannot write a 6 MB file to it again using FileZilla. If I format the SD card, I can write a 6 MB file using FileZilla once, then I cannot write to it again.

However, I can use explorer (not Internet Explorer), FireFTP in Firefox, or FTP in command prompt, I can FTP 6 MB files with no problem. It took about 12 minutes for a 6 MB file.

I’ve tried changing the DefaultBufferSize from 512 to 4096. It didn’t make any difference. It actually took longer to write. The most interesting thing was when I commented out the line that writes to the SD card, I still couldn’t use FileZilla to write a 6 MB file. But there was no difference in speed when I write to explorer or FireFTP with a 6 MB file. I think I commented out the correct line, because the file size would be 0 after it was done writing. The line I commented out is in FtpResponseStream.cs, public void ReadTo(Stream stream):

     stream.Write(buffer, 0, len);

I really don’t understand why it is not faster when I don’t write to the SD card and FileZilla still doesn’t work when I don’t write to the SD card.

I have played little bit with this FTP and it is quite stable.
I have a question regarding reliable finishing (Disposing) of this FTP server and starting it again e.g. when the ethernet cable is disconnected and connected again(as this is quite usual) or the Internet services(from provider) is stopped and started again.
I have tried to disconnect ethernet cable and there is some exception generated and catch by WorkerThread() but the WorkerThread() is then finished…
I have seen Dispose() in FTPListenerManager which can maybe solved these situation but I am not able to use it(honestly I do not have enough experience in C#)

Have you tried or do you have piece of code for solving these situations?

I have modified the FTP Server already on my side, to support connect and disconnect of Network cable, along with some other minor changes.
I will see when I can find some time to prepare and upload it.

1 Like

@ Reinhard Ostermeier - Thanks. It would be very helpful for me and I am guessing also for others if you can share these changes.

I have uploaded my latest version of stable FTP server.
How to handle network connect/disconnect, see the FtpServerTest application in the solution.
It’s quite simple.
Please be aware that the are a couple of exceptions in the output window, when the listener is stopped.
This is absolutely normal and all exceptions are handled correctly.

3 Likes

@ Reinhard Ostermeier - Thanks for sharing this changes. I will try to implement them.

I have tried it on my PCB based on G400(with ) and when I disconnect and connect Ethernet cable back then unhanded Exception is generated. I am not so experienced in network word so could you please tell me what could be wrong here.

Network availability: True
Network address changed
Network cable available:
IP: 192.169.1.222
SubnetMask: 255.255.255.0
Gateway: 192.169.1.1
DNS: 8.8.8.8
DNS: 8.8.4.4
Starting file system listener
Host IP: 192.169.1.222
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (21) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::bind [IP: 0000] ####
#### System.Net.Sockets.Socket::Bind [IP: 0016] ####
#### FtpServer.FtpListenerManager::WorkerThread [IP: 0033] ####
#### SocketException ErrorCode = 10048
Starting listener /SDROOT/
Starting file system listener
Starting listener /VIRTUALROOT/
#### SocketException ErrorCode = 10048
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10048
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

ErrorCode 10048 means the socket is still in use.
I had this once. Sometimes a socket - or better to say a port - is still locked, even the socket that was using it is closed.
There is a flag to ‘ignore’ this, when opening the socket.
I’m not sure why this happens in some cases, and in some it doesn’t. When I tested it yesterday on my G120, it didn’t happen.
I will see if I can add this flag to the FTP server listener port.

Thank you very much for your explanation.

That’s strange. I was just looking where the socket is created, and there it is:


That is exactly the code I wanted to add, but it is already there.
@ GHI: any chance this does not work on the G400, but on G120 it is working?

@ mhstr:
I saw why this gives an unhandled exception, but unfortunately I don't have the time to fix this now.
I report back here when I have fixed the issue. I also will test it on my G400 then.

@ Reinhard Ostermeier - It’s unlikely, but possible. We don’t really change the internals of networking between devices beyond buffer sizes and counts.

@ andre.m - it is true that it was quite fast - let say some seconds - up to 10, 15sec.? Is here any minimum time out between disconnect and connect?

@ Reinhard Ostermeier - It seems that the time between disconnect and connect must be quite long. I have tried 10 minutes and after this time it was possible to connect again, but maybe even lower time is OK(but some days ago I tried 2-3minutes and it was not OK so I am not sure what is the the min. time).
I am not too familiar with network settings and Sockets but it seems that it is somehow connected with TIME_WAIT network parameter although it should not be the case if “SocketOptionName.ReuseAddress” is used as I understood from some theory but maybe I am wrong.
So does “SocketOptionName.ReuseAddress” feature work properly on G400-S together with ENC28? Can somebody from GHI acknowledge it?

I would like to politely ask GHI once again if it would be possible from your side to test potential “SocketOptionName.ReuseAddress” exception issue on G400-S together with ENC28 after reconnecting Ethernet cable.
It generates: #### SocketException ErrorCode = 10048.
It is true that it was discovered during tests with last valid 2015 SDK, not with last 2016 Pre-release. Maybe you know if there was some change in new 2016 Pre-release but right now I am not able to test it with 2016 Pre-release because I have to urgently make some changes to application which works on last 2015 release and it is not possible to have installed both SDKs on one PC.

I am asking because I know there is still some work in progress on some network issues mentioned here:
https://www.ghielectronics.com/community/forum/topic?id=22141&page=14
so it would be perfect to correct also this one if this is a valid problem.
I am not so experienced in networking stuff but according @ Reinhard Ostermeier it works on G120 but the same code caused exceptions on G400 with ENC28 during my test of this very nice project of Stable FTP Server.