Discuss (FTP), (FTP on .NET CF) and (FTPS, SFTP and SCP).
no avatar
User

idelsoni

Posts

8

Joined

Fri Jan 28, 2011 8:12 am

How to detect connection is lost

by idelsoni » Thu Feb 17, 2011 11:24 pm

I'm using edtFTPFree. I would like to detect that my FTP server is no longer connected as in it has either gone off line or the cable has become unplugged.
I was hoping that there would be some kind of event that I could trap if this happens but I was unable to find one.

My second choice was to run a polling loop and keep checking the IsConnected property. This didn't work because even after I removed power from my FTP device/disconnected the cable IsConnected still kept returning true.

My next choice was to run a polling loop and send out a command, such as get files. hoping that if the server wasn't there, then an exception would be thrown. This didn't work either. I sent the command but the code just seemed to go off into the woods.

Is is possible to do this using edtFTP/Free? How about with one of the libraries that are for purchase?

Or do I have to back away from the FTP side and essentially use windows WMI to create a plug and play handler for ethernet?

Thanks,
Ira Idelson
no avatar
User

EDT Support

Posts

905

Joined

Mon Apr 26, 2004 3:03 pm

by EDT Support » Fri Feb 18, 2011 12:38 pm

When TCP connections are idle, no data is sent through them, so there is no way to detect if it's no longer possible to send data through them without actually trying it. So, this is what you need to do. Try calling pwd periodically. This will throw an IOException if the connection has been closed. Is that what you tried? It should work. If it doesn't then please post your code.

And yes, the pro version has a keep-alive feature which will send a command periodically and trigger a Closed event if the connection fails.

- Hans (EnterpriseDT)
no avatar
User

idelsoni

Posts

8

Joined

Fri Jan 28, 2011 8:12 am

by idelsoni » Sat Feb 19, 2011 12:00 am

Hi Hans,

I originally tried sending a GetFiles command which caused the hang. After pouring through the forums I found a posting with an issue similar to mine. This suggested using the GetSystemType() command. I tried that and was able to use the Exception to determine that the connection had been lost.

I after making my connection I use BeginInvoke to start a new thread where I surround the GetSystemType() command with a try/catch block. In the catch handler I fire a custom ConncetionLost event which closes the connection.

BTW, what is the difference between Close() and Close(true) where the second one abruptly closes the connection? When would I want to use the normal close and when would I want to use the abrupt close?


Thanks,
Ira Idelson
no avatar
User

EDT Support

Posts

905

Joined

Mon Apr 26, 2004 3:03 pm

by EDT Support » Sat Feb 19, 2011 5:34 am

Yes, using GetSystemType should be fine too.

Close() closes the connection 'nicely' by sending a QUIT command and waiting for a response, whereas Close(true) just kills the connection abruptly. From the client's point of view, there's not really any reason to be 'nice' and use Close(). Servers are written to be able to handle that since connections often drop out due to comms problems.

- Hans (EnterpriseDT)

Who is online

Users browsing this forum: No registered users and 25 guests

Powered by phpBB ® | phpBB3 Style by KomiDesign
cron