473,506 Members | 17,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting Disconnected Socket

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.

BTW. Socket.Connected returns true even tho it was disconnected

thank you in advance

- James

Jul 21 '05 #1
3 15100
Hi

"James Yang" <no****@microsoft.com> wrote in message
news:eh******************@TK2MSFTNGP11.phx.gbl...
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?
Are you sure that the client really closes the connection? Because if you
simply for example disconnect the network cable, or turn off the power of
the client, your side of the connection will not know about this and will
wait until the connection is restored or reset. I've encountered this many
times especially when connecting to our control devices connected via
TCP/IP. If such a device is reset, you won't know of it until you try to
send another message and receive a connection reset.

I believe I saw a way of doing this using IAsyncResult, and eventhandling
but..no article really explained how to use it properly.

I guess this won't solve your problem, but just google for something like
"asynchronous sockets .NET" and I guess you'll find something.
BTW. Socket.Connected returns true even tho it was disconnected
Well, this seems to be exactly the thing mentioned above...

thank you in advance

- James


Hope this helped,
Stefan
Jul 21 '05 #2
"James Yang" <no****@microsoft.com> wrote in message news:<eh**************@TK2MSFTNGP11.phx.gbl>...
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?

James -

If the remote computer gracefully disconnects the session, the
Socket.Receive() method will return with 0 bytes. You must detect that
to know that the remote end has disconnected:

int recv = sock.Receive(data);
if (recv == 0)
//remote client has disconnected
else
//remote client has sent data

Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Jul 21 '05 #3
On Fri, 26 Sep 2003 10:20:30 +0200, "Stefan Simek"
<si**********@kascomp.nospam.sk> wrote:
If such a device is reset, you won't know of it until you try to
send another message and receive a connection reset.


"...if the remote Socket was shut down gracefully, and all the data was
received, this method immediately returns zero..."

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclassreceivetopic1.htm

You must check for zero as the return value of either Receive or EndReceive.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Jul 21 '05 #4

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

Similar topics

1
7547
by: Tim Gosselin | last post by:
I am writing a tcp tunnel but cannot find a way of detecting when a socket shuts down its read end without writing to the socket. For testing the write end of the remote endpoint I just do a: if...
1
3611
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...
3
10597
by: Adam Clauss | last post by:
There seems to be various methods to determine when the remote client disconnects, but all of them I have seen are Synchronous. AKA: Right before you try to send or receive data, check. Is there...
2
2903
by: Vladimir Lushnikov | last post by:
Hi, I know there exist asynchronous socket methods for connecting, receiving/sending data and disconnecting - but how do you know when the socket has been terminated - can you assign an event...
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
371
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...
0
1703
by: Macca | last post by:
Hi, I am using an asynchronous socket server for my comms.I have multiple clients that connect to my server and hopefully stay connected sending data approx every 500ms. I want to make it...
3
8427
by: Jason Kristoff | last post by:
I'm trying to make something that once it is disconnected will automatically try to reconnect. I'll add some more features in later so it doesn't hammer the server but right now I just want to...
5
41592
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...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
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...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5617
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,...
1
5037
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...
0
4702
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...
0
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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 ...

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.