473,503 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting socket closed on TcpClient

Hi,

I have a project using the TcpClient and its associated NetworkStream.
Everything works well except for one condition which I haven't found any
information about dealing with:

How do I detected when the socket connection was closed on the
server-side?

That is, if my client connects to the server and everything is initialized
correctly the system runs well. But how can I tell if the server is shut
down or otherwise closes my connection without my involvement (that is
without my calling close on the TcpClient)?

Thanks,
Michael

Michael Kennedy
United Binary
http://www.unittesting.com
Jul 19 '05 #1
6 18454
Hello,

Thanks for the response. I was afraid that that was the case. I am using
try/catches to catch the errors but I would like to be able to let the user
know if the connection was closed by the server.

I know this can be done using straight winsock / Win32 API sockets and C++.
Is there an equivalent in .NET? Can this functionality be accessed using
some sort of interop along side the TcpClient (short of just using the C++
sockets via interop of course).

Thanks,
Michael

"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:cD**************@cpmsftngxa06.phx.gbl...
Hello Michael,

Thanks for your post.
But how can I tell if the server is shutdown or otherwise closes my
connection without my involvement?

I suggest you to use try-catch statement to catch the exceptions thrown
from tcp operations. Generally speaking, the client side will not be
notified when the connection was damanged on the server-side until it

fails to communicate with the service. For example, when the connection is
somehow not available, it will throw the following exception if you call
NetworkStream::Write or NetworkStream::Read on client side:

Unhandled Exception: System.IO.IOException: Unable to read data from the
transport connection. ---> System.Net.Sockets.SocketException: An existing
connection was forcibly closed by the remote host

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Jul 19 '05 #2
On my solution I send some sort of 'bye' message to the client telling it the connection is being dropped, and I do so from the server with no expectation of response. The client is coded such that receiveing that from the server is the end of things. Your server connection is dropped for only a few reasons, one of which you can't control inside your program (ISP/connectivity failure), the rest should be app errors that you should be able to trap and kick out your bye messages as the server does it's finishing work. The key is making sure the server can't by any means abruptly halt, but must terminate gracefully, and telling all the clients is part of that. Even so, the client must be insulated from connectivity failings because certainly the clients are much more likely to have them than the server (i.e. dialup drop) which is no different to the client than the server hanging up - the read/write fails and client must deal with it.

Chris Langsenkamp
Chat Live at http://www.cleverchat.net
--------------------------------------------------------------------------------

"Michael Kennedy [UB]" <mk******@REMOVETHIS.unitedbinary.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello,

Thanks for the response. I was afraid that that was the case. I am using
try/catches to catch the errors but I would like to be able to let the user
know if the connection was closed by the server.

I know this can be done using straight winsock / Win32 API sockets and C++.
Is there an equivalent in .NET? Can this functionality be accessed using
some sort of interop along side the TcpClient (short of just using the C++
sockets via interop of course).

Thanks,
Michael

"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:cD**************@cpmsftngxa06.phx.gbl...
Hello Michael,

Thanks for your post.
But how can I tell if the server is shutdown or otherwise closes my
connection without my involvement?

I suggest you to use try-catch statement to catch the exceptions thrown
from tcp operations. Generally speaking, the client side will not be
notified when the connection was damanged on the server-side until it

fails to communicate with the service. For example, when the connection is
somehow not available, it will throw the following exception if you call
NetworkStream::Write or NetworkStream::Read on client side:

Unhandled Exception: System.IO.IOException: Unable to read data from the
transport connection. ---> System.Net.Sockets.SocketException: An existing
connection was forcibly closed by the remote host

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Jul 19 '05 #3
Hi,

Thanks for the info. I know about interporating with the API using PInvoke.
But what I was asking for was a .NET way of acheiving the same
functionality. This limitation is not important enough for me to build whole
wrapper classes for the winsock API.

Also, I do catch the exceptions and they do contain the information about
the socket being closed. However, what I need is some way to determine if
the socket has been closed without traffic to the server. Consider that the
client connects to the server and everything is good. Sits there idle for
awhile, then the server is shutdown. Now the client will sit there for an
indefinite period of time (until it tries to talk again) before it knows the
server is gone.

Our C++ apps which use the Win32 API are notified immediately when the
server is shutdown. I was asking if there is the same functionality in the
..NET sockets. I guess the answer is no.

Thanks again,
Michael
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:Ko**************@cpmsftngxa06.phx.gbl...
Hello Michael,

Thanks for your reply. I'd like to share the following information with you:
1. According to the SocketException, it's clearly indicate that the
connection was closed by the server.
"System.Net.Sockets.SocketException: An existing connection was forcibly
closed by the remote host"

2. You can also call winsock APIs directly by using Platform Invoke. Please refer to the following MSDN article:

Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconconsumingunmanageddllfunctions.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Jul 19 '05 #4
Hi Michael,

Thanks for your response. I am now performing further research on this
issue, and will come back with my information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
Jul 19 '05 #5
Hello Michael,

Now I'd like to share the following information with you. Generally
speaking, we should set the SO_KEEPALIVE option (by using setsockopt) to
monitor the connection. Please refer to the following KB article:

How to Determine Loss of Client/Server Connection
http://support.microsoft.com/default...b;en-us;140325

In .NET Framework, please refer to the corresponding KeepAlive option:

SocketOptionName Enumeration
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemnetsocketssocketoptionnameclasstopic.as p

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
Jul 19 '05 #6
Hi,

Thanks for the response. That does look like it might be helpful to me. I'll
let you know how it goes.

Thanks,
Michael
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:FO**************@cpmsftngxa06.phx.gbl...
Hello Michael,

Now I'd like to share the following information with you. Generally
speaking, we should set the SO_KEEPALIVE option (by using setsockopt) to
monitor the connection. Please refer to the following KB article:

How to Determine Loss of Client/Server Connection
http://support.microsoft.com/default...b;en-us;140325

In .NET Framework, please refer to the corresponding KeepAlive option:

SocketOptionName Enumeration
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemnetsocketssocketoptionnameclasstopic.as p

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Jul 19 '05 #7

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

Similar topics

3
10595
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...
0
1677
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished...
0
1170
by: Jonathan | last post by:
Hi, how to detect when an asynchronous socket is closed? Thanks!
0
298
by: Michael Kennedy [UB] | last post by:
Hi, I have a project using the TcpClient and its associated NetworkStream. Everything works well except for one condition which I haven't found any information about dealing with: How do I...
3
3965
by: Dirk Reske | last post by:
Hello, in msdn stands, that the socket.available property can fire a SocketException when the remote machine has closed the connection. why can? I have to check the number of receivable bytes...
4
5210
by: schwehr | last post by:
Hi All, I've tried to RTFM this and am having no luck. First off, I am using Mac OSX 10.4.7 with python 2.4.2 from fink. I am trying to connect to a server that should be rejecting...
1
1402
by: Jordi | last post by:
Hi, I have my socket class using tcpClient framework 2.0... The socket is running perfectly but I've a problem for close the socket connection with the server! I try with this: Stm is the...
4
7567
by: Andrew Jackson | last post by:
I am writing a newsgroup client. I have the protocol figured out. But I get slow transfer speeds off any of the network objects read the data from For example one of the commands for a news...
5
41586
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
7203
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
7087
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...
0
7281
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,...
0
7462
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...
0
5579
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,...
0
4675
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
3168
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
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
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.