Telling when a Socket is Disconnected

N

Nagash01WS6

Im working on writing an FTP application in C# to get a good
feeling for the .NET language and how threading / networking
classes all work. Thus far I have it working for the most
part in PASV mode. The only problem I have encountered is
when the server ends the connection, the
socket cant tell and attempts to continue reading data
infinitely.

Basically... in PASV mode, the FTP server opens a random
port > 1024 and the client connects to that port. When file
transfer is completed the server drops the connection on
that port. However... the port stays open for another
connection for data transfer.

So... what I think is happening is when the connection is
dropped, the socket reconnects and attempts to continue
reading data. There is no more data. I unfortunately can
not base breaking out of the loop based upon data being
available on the socket because it may take packets
longer than others to arrive.

There is a success message transferred to the control
connection of the FTP client upon success of the file
transfer. That means that the server has sent all the
packets out... but doesnt mean the client has recieved them
all.

I see my solutions as being setting a boolean variable upon
receipt of the success message from the server... but how
can I tell when the data is done???? Would I need to have a
timeout after an arbitrary amount of time??? Is there any
way to tell that the connection has dropped???
Apparantly the socket is reconnecting on its own after the
completetion of the data transfer... is there a way to set
it to NOT do this????
 
C

Champika Nirosh

I didn't read the whole posting .. but I guess you are missing the ping..
that is you have to have a ping message from the client to the server going
at a periodic rate and a timer should keep on looking at it and check
whether the ping message successfully recieved the server if the response
didn't come after significant amount of time period that say that the
connection is dropped.

Hope this help you

Nirosh.
 
K

karl

Nagash01WS6 said:
Im working on writing an FTP application in C# to get a good
feeling for the .NET language and how threading / networking
classes all work. Thus far I have it working for the most
part in PASV mode. The only problem I have encountered is
when the server ends the connection, the
socket cant tell and attempts to continue reading data
infinitely.

Basically... in PASV mode, the FTP server opens a random
port > 1024 and the client connects to that port. When file
transfer is completed the server drops the connection on
that port. However... the port stays open for another
connection for data transfer.

So... what I think is happening is when the connection is
dropped, the socket reconnects and attempts to continue
reading data. There is no more data. I unfortunately can
not base breaking out of the loop based upon data being
available on the socket because it may take packets
longer than others to arrive.

There is a success message transferred to the control
connection of the FTP client upon success of the file
transfer. That means that the server has sent all the
packets out... but doesnt mean the client has recieved them
all.

I see my solutions as being setting a boolean variable upon
receipt of the success message from the server... but how
can I tell when the data is done???? Would I need to have a
timeout after an arbitrary amount of time??? Is there any
way to tell that the connection has dropped???
Apparantly the socket is reconnecting on its own after the
completetion of the data transfer... is there a way to set
it to NOT do this????

if you're using a networkstream, try something like this...
// client is a Socket
//Console.WriteLine("Connection accepted...");
ns = new NetworkStream(client);
bool bState, bConnected = true;

while (bConnected)
{
if (ns.DataAvailable) this.GetMessage();
bState = client.Poll(1, SelectMode.SelectRead);

if (bState && client.Available == 0)
bConnected = false;
else
bConnected = true;

System.Threading.Thread.Sleep(100);
}
 
M

Mike Blake-Knox

Nagash01WS6 said:
So... what I think is happening is when the connection is
dropped, the socket reconnects and attempts to continue
reading data. There is no more data.

Traditionally, you detect a socket connection failure by looking for
the receipt of 0 bytes of data.

Hope this helps.

Mike Blake-Knox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top