473,482 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to use the Socket "Connected" property properly?

I have an application that opens a socket and connects to another
application listening over a port. The problem I am encountering is that
when the listening application is closed my application cannot detect it to
take appropriate action. I am using "Connected" property of the Socket
class, but it seems this property does not reflect the true state of the
socket connection.

Here is the code snippet. Basically, I am checking the "Socket.Connected"
property in a while loop and want to drop out of the loop as soon as the
listening application is closed. The problem is when I close the listening
application, the while loop continues happily. Any suggestions on how to
make the Socket.Connected property to reflect true state of socket
connection? Any other ideas?

Thanks in advance,

Socket MySocket = null;
IPEndPoint Ep;

MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
Ep = new IPEndPoint(IPAddress, Port);

// connect to another application listening
MySocket.Connect (EpF);

while (MySocket.Connected )
{
// use socket to send/receive data
// Drop out of this loop if the listening
// application is closed.

}
Nov 16 '05 #1
1 16386
Welcome to TCP...

The way you discover that the remote end has closed a connection is by
attempting to read from it. The read attempt returns no data, indicating
that the connection has closed.

This is not unique to .NET by the way - most sockets do this. (The same
issue exists on Linux for example.)

The reason for this is that because of the way TCP works, it is valid for
the client to continue to send data to the server even though the server may
have closed the connection at its end. All it really means for the server
to close the connection is that it is shutting down its ability to transmit
data to the client. The connection is in a state sometimes described as
'half-open'.

So that's it's the Read operation that tells you the server has closed the
connection - the fact that the server has closed its connection simply means
that you won't be receiving any more data from it. This is not necessarily
going to prevent you from sending it more data, which is why the connection
doesn't appear to be closed at this point.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Dr. J" wrote:
I have an application that opens a socket and connects to another
application listening over a port. The problem I am encountering is that
when the listening application is closed my application cannot detect it
to
take appropriate action. I am using "Connected" property of the Socket
class, but it seems this property does not reflect the true state of the
socket connection.

Here is the code snippet. Basically, I am checking the "Socket.Connected"
property in a while loop and want to drop out of the loop as soon as the
listening application is closed. The problem is when I close the
listening
application, the while loop continues happily. Any suggestions on how to
make the Socket.Connected property to reflect true state of socket
connection? Any other ideas?

Thanks in advance,

Socket MySocket = null;
IPEndPoint Ep;

MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
Ep = new IPEndPoint(IPAddress, Port);

// connect to another application listening
MySocket.Connect (EpF);

while (MySocket.Connected )
{
// use socket to send/receive data
// Drop out of this loop if the listening
// application is closed.

}

Nov 16 '05 #2

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

Similar topics

1
2524
by: ETYC | last post by:
I am not sure if this is a bug or not but it seems strange to me. If I set the tcp socket blocking to false and do a connect, then I can catch the socketexception. If the exception is...
0
1570
by: Dmitry Klymenko | last post by:
Please see the Socket.Close page in MSDN. There are such line in Remarks: "The Close method closes the the remote host connection and releases all managed and unmanaged resources associated with...
5
44937
by: Morten | last post by:
How do I detect if a client socket is no longer connected to the listen tcp socket ? I have tried with (just an example): --------------------- Socket tcpSocket; ...
0
1659
by: et-yc | last post by:
I am not sure if this is a bug or not but it seems strange to me. If I set the tcp socket blocking to false and do a connect, then I can catch the socketexception. If the exception is...
1
6745
by: D. Wichert | last post by:
Hi folks, I use a TcpClient object to connect to a local (asynchron) socket server. Once the connection is established, I ask every second if there is a new entry in the socket queue. If the...
2
7529
by: Ben | last post by:
I need to send data from a client to a server. In the server code I have: s = ServerSocket.Accept() If (s.Connected = False) Then MsgBox("Unable to connect", , "Server Error") Exit Sub End...
7
16905
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
14
5885
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
3
3035
by: A. W. Dunstan | last post by:
I'm creating a socket as follows: m_networkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_networkSocket.LingerState = new LingerOption(true, 1);...
0
7075
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
6928
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
7089
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
7130
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
7088
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
5388
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
4811
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
4516
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
3032
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...

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.