473,574 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ 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.Connect ed"
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.Connecte d property to reflect true state of socket
connection? Any other ideas?

Thanks in advance,

Socket MySocket = null;
IPEndPoint Ep;

MySocket = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
Ep = new IPEndPoint(IPAd dress, Port);

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

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

}
Nov 16 '05 #1
1 16427
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.Connect ed"
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.Connecte d property to reflect true state of socket
connection? Any other ideas?

Thanks in advance,

Socket MySocket = null;
IPEndPoint Ep;

MySocket = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
Ep = new IPEndPoint(IPAd dress, Port);

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

while (MySocket.Conne cted )
{
// 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
2537
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 WSAEWOULDBLOCK, then I do a poll with timeout to see if I can connect to the remote host. When the poll returns true, the socket Connected property is still...
0
1582
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 the Socket. Upon closing, the Connected property is set to false." Please note the last (bold) sentence. And see the example (on same page):...
5
44965
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; while(tcpSocket.Connected)
0
1669
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 WSAEWOULDBLOCK, then I do a poll with timeout to see if I can connect to the remote host. When the poll returns true, the socket Connected property is still...
1
6780
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 connection was established successfully at the beginning and ends for some reason after a certain time, I have to reconnect to the socket server. ...
2
7551
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 If While (s.Connected = True)
7
16927
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 write an helper method that will tell if the socket is connected or not , but it's not working good continue to tell me that the socket is...
14
5903
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 connected. Any idea? Thanks.
3
3041
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); m_networkSocket.Blocking = true; m_networkSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);...
0
7814
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...
0
8070
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. ...
1
7827
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...
0
8107
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6469
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5631
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...
0
5309
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...
0
3760
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1353
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.