473,385 Members | 1,934 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,385 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 15078
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.