473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 15084
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.