472,374 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 16006
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
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
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
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
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
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
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
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
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
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);...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.