Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Net.Socket.Connected

System.Net.Socket.Connected

Scheduled Pinned Locked Moved C#
debuggingcsharpcomdesign
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason McBurney
    wrote on last edited by
    #1

    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

    L N 2 Replies Last reply
    0
    • J Jason McBurney

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • L 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

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        led mike wrote:

        VG6, VV6

        LMFAO! :-D

        Dave

        1 Reply Last reply
        0
        • J Jason McBurney

          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

          N Offline
          N Offline
          nelsonpaixao
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups