473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting Disconnect on TCP Socket


I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.

Socket.Connecte d still returns true at this point, which I know is
expected behavior, so when I reach this state, I begin sending on this
socket every few seconds until an error is generated.

After the exception is raised, Socket.Connecte d returns false.
However, I cannot connect again to this socket. Calls to BeginConnect
fail with a message "A connect request was made on an already
connected socket."

How do I work around this, short of recreating the socket?

--Bruce
Nov 16 '05 #1
6 16642
"Bruce Vander Werf" <br*****@hotmai l.com> wrote in message
news:5f******** *************** *********@4ax.c om...

I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.


Did you immediately call Close() against the socket at this point? If
not, this might be your problem. Don't bother trying to send stuff across
this socket after EndReceive() has returned zero. It's like beating a dead
horse (that's just an expression).

If I were you, I'd then call Dispose() on the socket, stop referencing
it, and let the GC get rid of it. Next time you need to initiate/accept a
new connection, just use a new Socket object. I know you said you were
against the idea of creating a new socket, but hey, why not?
Nov 16 '05 #2
Hi, Bruce

This is normal. After exception you usually can't use same socket. You have
to close/destroy old one and create new. I mean of course network-related
exception, not division by zero :-)

HTH
Alex

"Bruce Vander Werf" <br*****@hotmai l.com> wrote in message
news:5f******** *************** *********@4ax.c om...

I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.

Socket.Connecte d still returns true at this point, which I know is
expected behavior, so when I reach this state, I begin sending on this
socket every few seconds until an error is generated.

After the exception is raised, Socket.Connecte d returns false.
However, I cannot connect again to this socket. Calls to BeginConnect
fail with a message "A connect request was made on an already
connected socket."

How do I work around this, short of recreating the socket?

--Bruce

Nov 16 '05 #3
David,

it's not always like this. I've seen situations when endreceive delivered 0
bytes, however beginreceive issued at that point was getting more data. It
really depends on how remote end behaves. If remote server is slow - you can
get 0 bytes while there are data available.
If you know what you should get from remote server you can check if you got
everything, for example using end-of-file byte or some tag line. I've seen
situations when I was receiving 5-10MB in chunks of 100-200K with delays of
several seconds between chunks. Until exception happens or
socket.Connecte d!=true socket could be considered as working and connected.

HTH
Alex

"David Sworder" <ds******@cts.c om> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
"Bruce Vander Werf" <br*****@hotmai l.com> wrote in message
news:5f******** *************** *********@4ax.c om...

I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.


Did you immediately call Close() against the socket at this point? If
not, this might be your problem. Don't bother trying to send stuff across
this socket after EndReceive() has returned zero. It's like beating a dead
horse (that's just an expression).

If I were you, I'd then call Dispose() on the socket, stop referencing
it, and let the GC get rid of it. Next time you need to initiate/accept a
new connection, just use a new Socket object. I know you said you were
against the idea of creating a new socket, but hey, why not?

Nov 16 '05 #4
Hi Alex,
it's not always like this. I've seen situations when endreceive delivered 0 bytes, however beginreceive issued at that point was getting more data. It
really depends on how remote end behaves. If remote server is slow - you can get 0 bytes while there are data available.
Are you sure about this? I just double-checked the documentation for
EndReceive() and it looks like a return value of 0 indicates that the remote
system has Shutdown the connection.

Consider an example where a client connects to a server and doesn't send
any data for five minutes. The server calls BeginReceive() immediately after
the connection is accepted. The first callback won't be even be made for 5
minutes. When the callback is finally made, there will be at least 1 byte of
data to receive. In other words, EndReceive() should return a value greater
than zero. If for some reason the client terminates early (say after two
minutes), the callback will be made and the server's EndReceive() call will
return zero. At that point, are you saying that it's possible for further
data to be in the pipeline? I wouldn't have thought that this is the case
since the socket is already shutdown. I would think that the socket should
immediately be closed and disposed at this point.
If you know what you should get from remote server you can check if you got everything, for example using end-of-file byte or some tag line. I've seen
situations when I was receiving 5-10MB in chunks of 100-200K with delays of several seconds between chunks.


...but your EndReceive() should never have returned zero between chunks,
right? If it did, wouldn't this imply that the client had dropped the
connection?

David
Nov 16 '05 #5
David,

MSDN says only:

-quote-
"If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the EndReceive method will
complete immediately and return zero bytes"
-unquote-

It says nothing about 0 bytes meaning always shutdown with complete receipt.
It really depends on server on remote end. 0 could mean shutdown, lack of
data, not available data and other things, but not always shutdown.

HTH
Alex
"David Sworder" <ds******@cts.c om> wrote in message
news:uH******** ******@TK2MSFTN GP09.phx.gbl...
Hi Alex,
it's not always like this. I've seen situations when endreceive delivered
0
bytes, however beginreceive issued at that point was getting more data.
It really depends on how remote end behaves. If remote server is slow - you

can
get 0 bytes while there are data available.


Are you sure about this? I just double-checked the documentation for
EndReceive() and it looks like a return value of 0 indicates that the

remote system has Shutdown the connection.

Consider an example where a client connects to a server and doesn't send any data for five minutes. The server calls BeginReceive() immediately after the connection is accepted. The first callback won't be even be made for 5
minutes. When the callback is finally made, there will be at least 1 byte of data to receive. In other words, EndReceive() should return a value greater than zero. If for some reason the client terminates early (say after two
minutes), the callback will be made and the server's EndReceive() call will return zero. At that point, are you saying that it's possible for further
data to be in the pipeline? I wouldn't have thought that this is the case
since the socket is already shutdown. I would think that the socket should
immediately be closed and disposed at this point.
If you know what you should get from remote server you can check if you got
everything, for example using end-of-file byte or some tag line. I've seen situations when I was receiving 5-10MB in chunks of 100-200K with delays

of
several seconds between chunks.


...but your EndReceive() should never have returned zero between

chunks, right? If it did, wouldn't this imply that the client had dropped the
connection?

David

Nov 16 '05 #6
aha! Thanks for the clarification.

"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:uV******** ******@TK2MSFTN GP10.phx.gbl...
David,

MSDN says only:

-quote-
"If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the EndReceive method will complete immediately and return zero bytes"
-unquote-

It says nothing about 0 bytes meaning always shutdown with complete receipt. It really depends on server on remote end. 0 could mean shutdown, lack of
data, not available data and other things, but not always shutdown.

HTH
Alex
"David Sworder" <ds******@cts.c om> wrote in message
news:uH******** ******@TK2MSFTN GP09.phx.gbl...
Hi Alex,
it's not always like this. I've seen situations when endreceive delivered
0
bytes, however beginreceive issued at that point was getting more
data.
It really depends on how remote end behaves. If remote server is slow -
you can
get 0 bytes while there are data available.
Are you sure about this? I just double-checked the documentation for
EndReceive() and it looks like a return value of 0 indicates that the

remote
system has Shutdown the connection.

Consider an example where a client connects to a server and doesn't

send
any data for five minutes. The server calls BeginReceive() immediately

after
the connection is accepted. The first callback won't be even be made for 5 minutes. When the callback is finally made, there will be at least 1 byte of
data to receive. In other words, EndReceive() should return a value

greater
than zero. If for some reason the client terminates early (say after two
minutes), the callback will be made and the server's EndReceive() call

will
return zero. At that point, are you saying that it's possible for

further data to be in the pipeline? I wouldn't have thought that this is the case since the socket is already shutdown. I would think that the socket should immediately be closed and disposed at this point.
If you know what you should get from remote server you can check if
you got
everything, for example using end-of-file byte or some tag line. I've seen situations when I was receiving 5-10MB in chunks of 100-200K with

delays of
several seconds between chunks.


...but your EndReceive() should never have returned zero between

chunks,
right? If it did, wouldn't this imply that the client had dropped the
connection?

David


Nov 16 '05 #7

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

Similar topics

3
15124
by: James Yang | last post by:
Hi, I am using Sockets to connect to a remote computer and send data using Socket.Send() and receive using Socket.Receive() (block mode) . for somereason tho, when the client disconnects the Socket.Receive() just passes without any exception. Is there any way to detect disconnection using Sockets? I believe I saw a way of doing this using IAsyncResult, and eventhandling but..no article really explained how to use it properly.
2
1828
by: yaron | last post by:
i get an SocketException exception when using socket select method. how do i know what is the socket that cause the exception ? can i get this info from the exception instance ? Thanks.
1
700
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
3
2572
by: xincmu | last post by:
I built a very basic server-client in C#. When my server disconnects from the client, the socket at the server is already disconnected, but the socket the client remains open. I used the sample code in .Net's documentation to disconnect: socket.Shutdown( SocketShutdown.Both ); socket.Disconnect( false ); I also tried
3
14406
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the server socket, the client is able to connect, but cannot send anything out. It seems that the Socket.Connected is false but I received no disconnection event. Any idea on how to solve the problem? Thanks.
0
1204
by: =?Utf-8?B?RGllZ28=?= | last post by:
The Connected property of the Socket class does not return false when the Socket no longer is connected to the host connection. Is there another way to verify that the Socket is disconnected? tnx
4
4307
by: Adam Clauss | last post by:
A while back I posted regarding a problem we were having with one of our applications which was randomly crashing. Monitoring memory usage revealed a spike in nonpaged pool memory just prior to the crash each time. We finally think we have narrowed down the cause of this to a user (located semi-remotely) who would connect into our system and disconnect "ungracefully" (literally, by pulling his network cable). Connections here are all...
5
41850
by: tichi | last post by:
I'm writing a program that in its most simplest form acts as a proxy between a local client and a remote server. But I cannot figure out how to detect when either the connection to the client is close or the connection to the server is closed. I've tried looking at socket.connected - it always returns true, even when the client program has disconnected. I've tried getting an exception from the networkstream.DataAvailable, it always just reads...
1
4675
by: s3raph | last post by:
Hi, I'm having trouble detecting the client machine connection lost when the client physically pushes the power or restart button on their computer. Using the code from Plater in the following thread: http://bytes.com/topic/net/answers/828133-c-net-detecting-socket-closed I have managed to detect disconnect by the client when the client window form is closed, but if the client computer switches off or powers off, it doesn't detect a...
0
8305
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
8823
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
8726
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...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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
5632
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
4151
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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

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.