473,672 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Telling when a Socket is Disconnected

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????
Nov 15 '05 #1
3 2159
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.

"Nagash01WS 6" <ia*****@fooz.c om> wrote in message
news:Xn******** *************** ****@65.32.1.8. ..
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????

Nov 15 '05 #2
Nagash01WS6 <ia*****@fooz.c om> wrote in message news:<Xn******* *************** *****@65.32.1.8 >...
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.WriteLi ne("Connection accepted...");
ns = new NetworkStream(c lient);
bool bState, bConnected = true;

while (bConnected)
{
if (ns.DataAvailab le) this.GetMessage ();
bState = client.Poll(1, SelectMode.Sele ctRead);

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

System.Threadin g.Thread.Sleep( 100);
}
Nov 15 '05 #3
In article <Xn************ *************** @65.32.1.8>, Nagash01WS6
wrote:
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

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1937
by: Jacob Lee | last post by:
I'm getting a rather bizarre error while using the socket module. If I start out disconnected from the net and then connect while the program or interpreter session is open, I do not always gain the ability to resolve or connect to hosts by name. Here is the problematic scenario: <Unplug network cable> >>> from socket import * >>> gethostbyname('google.com') # Fails as expected Traceback (most recent call last):
1
3788
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
6
3205
by: Robert Mens | last post by:
Hi, I've got this problem with this project i am working on, i am a bit new to c so i don't know why this happens. string is read from a telnet socket. Here's the piece of code that segfaults: struct login_usertable{
2
7878
by: dream machine | last post by:
Hi all, I'm try to check if the one client socket is really connected at my SocketServer . I build Client Socket with BeginAccept() , and I want to receive data with BegenReceive() call ! The BeginReceive and BeginAccept work very nice . <Code type="C#">
1
3656
by: Ashwin Kambli | last post by:
Hi, I have a simple socket application. There is a server, and there are many TCP connections to the server. Now, if any one of those connections is lost, is there a way of triggering an event on the server for this. What I need is to set up a delegate to handle this disconnection (Delegate has some cleanup code). As soon as the socket is disconnected, the event should call the delegate. Thanking you, Ashwin Kambli
4
1709
by: DaTurk | last post by:
I want to know when a socket disconnects so I can attempt to reconnect. Is there any special type of pattern to doing this? Is there an evnt raised?
0
1620
by: craze3 | last post by:
I have programmed an XML Socket Server to communicate with Flash. It doesn't print out the messages it has received from the flash until the server has been shut down. Any ideas? The part with the problem: //Create streams BufferedReader data_in = new BufferedReader( new InputStreamReader(incoming.getInputStream())); PrintWriter data_out = new...
1
1886
by: Miroslav Endys | last post by:
I have two apps. Client and Server (both are windows console apps). Im using Client Socket of this definition AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp and Im using async communication with the Server Socket. I have no opened NetworkStream. In case, I kill my client application (via red cross) the server application can see, that client has been disconnected. In case, I shutdown the client socket and close it on...
4
15277
by: keithseah | last post by:
Hi all, i've been having this problem and its kiiling me! i'm a newbie at this so i hope someone would be able to help me. picture link: http://i98.photobucket.com/albums/l272/rachelyeo/ErrorWebApplication2.jpg this pops up whenever i click on the Disconnect button after i have connected. these are the following codes for the program.
0
8407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8932
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5708
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4420
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.