473,898 Members | 3,407 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.Connecte d returns true even tho it was disconnected

thank you in advance

- James

Jul 21 '05 #1
3 15149
Hi

"James Yang" <no****@microso ft.com> wrote in message
news:eh******** **********@TK2M SFTNGP11.phx.gb l...
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
"asynchrono us sockets .NET" and I guess you'll find something.
BTW. Socket.Connecte d 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****@microso ft.com> wrote in message news:<eh******* *******@TK2MSFT NGP11.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(da ta);
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**********@k ascomp.nospam.s k> 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/frlrfsystemnets ocketssocketcla ssreceivetopic1 .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
7668
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 not sock.recv(buffsize) I cannot write to the socket and check if send returns 0 though, because that involves sending data out of the which may not have come in yet. When I try polling the socket with:
1
3672
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
3
10616
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 no way to do raise an event for this? That way you know as soon as the client disconnects? There are events for Accepting, Connecting, Receiving, Sending... why not one for disconnecting? -- Adam Clauss cabadam@tamu.edu
2
2929
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 handler or a delegate as to when the socket is disconnected - either gracefully from remote or shut down by "brute force"? In short, something like this:
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 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.
0
1729
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 robust as possible and was wondering how I would efficently detect a socket failure/disconnection and recover from it. Would it be up to the client to re-establish the connection?
3
8495
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 keep it simple and get that part working. The problem is that when I use sock.close I get an error message of Bad File Descriptor and if I either use shutdown or just go straight to reconnecting I get: Transport endpoint is already connected ...
5
42318
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...
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9842
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
11265
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...
1
10952
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8036
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
7189
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
5882
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
6078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3308
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.