System.Net.Socket.Connected
-
I am porting a VB6 winsock.ocx based library to a .Net Sockets implementation, and I am not the most savvy sockets programmer. The architecture of the communication system was designed long before I got here; so changing the overall design is not an option. Moreover, this system has a low frequency of tcp traffic, so the Socket.Connected will almost always be out-of-date. The fundamental issue is the connected property of the socket. It does not seem to be funcitonal; however, I have found others that have had similar issues http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx[^] When I stepping-through the code in the debugger, unplug the network at the right moment, and view the sock object with the quick watcher, it shows me the value of sock.Connected has changed to false as expected. However, while running the code below, in debug mode but not stepping-through, the behavior of sock.Connected is different (when I unplug the wire, it does not update to false). Thoughts?
private bool isConnected(){
bool rVal = false;
//an alternate way to refresh the connected status of the sock.
try
{
bool blcking = sock.Blocking;
try
{
byte[] junk = new byte[2];
sock.Blocking = false;
sock.Send(junk, 1, 0, SocketFlags.None);
rVal = sock.Connected;
}
finally
{
sock.Blocking = blcking;
}
}
catch (SocketException e)
{
rVal = e.SocketErrorCode == SocketError.WouldBlock;
}
return rVal;
}You can only be young once. But you can always be immature. - Dave Barry
-
I am porting a VB6 winsock.ocx based library to a .Net Sockets implementation, and I am not the most savvy sockets programmer. The architecture of the communication system was designed long before I got here; so changing the overall design is not an option. Moreover, this system has a low frequency of tcp traffic, so the Socket.Connected will almost always be out-of-date. The fundamental issue is the connected property of the socket. It does not seem to be funcitonal; however, I have found others that have had similar issues http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx[^] When I stepping-through the code in the debugger, unplug the network at the right moment, and view the sock object with the quick watcher, it shows me the value of sock.Connected has changed to false as expected. However, while running the code below, in debug mode but not stepping-through, the behavior of sock.Connected is different (when I unplug the wire, it does not update to false). Thoughts?
private bool isConnected(){
bool rVal = false;
//an alternate way to refresh the connected status of the sock.
try
{
bool blcking = sock.Blocking;
try
{
byte[] junk = new byte[2];
sock.Blocking = false;
sock.Send(junk, 1, 0, SocketFlags.None);
rVal = sock.Connected;
}
finally
{
sock.Blocking = blcking;
}
}
catch (SocketException e)
{
rVal = e.SocketErrorCode == SocketError.WouldBlock;
}
return rVal;
}You can only be young once. But you can always be immature. - Dave Barry
Jason McBurney wrote:
Thoughts?
My thought is, I hope you are hung up on the concept of interrogating the socket for it's state because that's what you saw in the VG6 *gulp* uh, mess. I hope you appreciate how difficult it is for me to even type VV6, see I just can't do it. :) Using exception handling you can can just execute your code and catch exceptions indicating the socket is not connected. You should search around the internet, Sockets.com has been around for ever. Look for examples like the one in the documentation for Socket.Send[^]
led mike
-
Jason McBurney wrote:
Thoughts?
My thought is, I hope you are hung up on the concept of interrogating the socket for it's state because that's what you saw in the VG6 *gulp* uh, mess. I hope you appreciate how difficult it is for me to even type VV6, see I just can't do it. :) Using exception handling you can can just execute your code and catch exceptions indicating the socket is not connected. You should search around the internet, Sockets.com has been around for ever. Look for examples like the one in the documentation for Socket.Send[^]
led mike
-
I am porting a VB6 winsock.ocx based library to a .Net Sockets implementation, and I am not the most savvy sockets programmer. The architecture of the communication system was designed long before I got here; so changing the overall design is not an option. Moreover, this system has a low frequency of tcp traffic, so the Socket.Connected will almost always be out-of-date. The fundamental issue is the connected property of the socket. It does not seem to be funcitonal; however, I have found others that have had similar issues http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx[^] When I stepping-through the code in the debugger, unplug the network at the right moment, and view the sock object with the quick watcher, it shows me the value of sock.Connected has changed to false as expected. However, while running the code below, in debug mode but not stepping-through, the behavior of sock.Connected is different (when I unplug the wire, it does not update to false). Thoughts?
private bool isConnected(){
bool rVal = false;
//an alternate way to refresh the connected status of the sock.
try
{
bool blcking = sock.Blocking;
try
{
byte[] junk = new byte[2];
sock.Blocking = false;
sock.Send(junk, 1, 0, SocketFlags.None);
rVal = sock.Connected;
}
finally
{
sock.Blocking = blcking;
}
}
catch (SocketException e)
{
rVal = e.SocketErrorCode == SocketError.WouldBlock;
}
return rVal;
}You can only be young once. But you can always be immature. - Dave Barry
I don´t know if you got the anser to your question, if not try to download some chat applications here in codeproject. I started to built one but i didn´t finished it yet, and i didn´t find the code to acomplish all the things i have in my mind. I´am not an advanced programmer but, i see in your post some similar code to the one i typed (and read before). Good Luck :laugh: