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. getting the local port as netstat in C# + small doubt on network connection

getting the local port as netstat in C# + small doubt on network connection

Scheduled Pinned Locked Moved C#
csharpsysadminhelpquestion
4 Posts 2 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.
  • M Offline
    M Offline
    manustone
    wrote on last edited by
    #1

    Hi All This post got actually 2 questions but related each other. I hope you can help me even just solving one of the 2. In Windows I usually call NETSTAT to have information regarding the status of my network connections. I am interested in getting the data below produced by Netstat using C#. *Local Address *The state of the connection. Active Connections Proto Local Address Foreign Address State TCP ITA-2317A:3377 server1.mylan.co.uk:50506 FIN_WAIT_2 TCP ITA-2317A:3378 server1.mylan.co.uk:50507 FIN_WAIT_2 I am interested in catching the FIN_WAIT_2 state that sometimes is causing me some problems. My C# application sometimes leaves the network connection in this state..do you know some possible workaround? I am using TcpClient class and I use Close() method..after I do this what check can I do to make sure that my connection is not in the FIN_WAIT_2 state?

    L 1 Reply Last reply
    0
    • M manustone

      Hi All This post got actually 2 questions but related each other. I hope you can help me even just solving one of the 2. In Windows I usually call NETSTAT to have information regarding the status of my network connections. I am interested in getting the data below produced by Netstat using C#. *Local Address *The state of the connection. Active Connections Proto Local Address Foreign Address State TCP ITA-2317A:3377 server1.mylan.co.uk:50506 FIN_WAIT_2 TCP ITA-2317A:3378 server1.mylan.co.uk:50507 FIN_WAIT_2 I am interested in catching the FIN_WAIT_2 state that sometimes is causing me some problems. My C# application sometimes leaves the network connection in this state..do you know some possible workaround? I am using TcpClient class and I use Close() method..after I do this what check can I do to make sure that my connection is not in the FIN_WAIT_2 state?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This probably can help you: http://support.microsoft.com/kb/821625[^]

      M 1 Reply Last reply
      0
      • L Lost User

        This probably can help you: http://support.microsoft.com/kb/821625[^]

        M Offline
        M Offline
        manustone
        wrote on last edited by
        #3

        Thanks! I am testing this right now..I have a small doubt bit trivial. I use embed the NetworkStream in a StreamReader like this

        NetworkStream aStream = _client.GetStream();
        StreamReader reader = new StreamReader(aStream);

        calling reader.Close() does it safely close also the NetworkStream underlying or do I have to keep a reference to the NetworkStream and close it explicitly?

        L 1 Reply Last reply
        0
        • M manustone

          Thanks! I am testing this right now..I have a small doubt bit trivial. I use embed the NetworkStream in a StreamReader like this

          NetworkStream aStream = _client.GetStream();
          StreamReader reader = new StreamReader(aStream);

          calling reader.Close() does it safely close also the NetworkStream underlying or do I have to keep a reference to the NetworkStream and close it explicitly?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          This is normally what I do:

          TcpClient tcpClient = new TcpClient();
          tcpClient.Connect("myserver", 1234);
          NetworkStream networkStream = tcpClient.GetStream();
          //... Read or Write here...
          networkStream.Close();
          networkStream.Dispose();
          tcpClient.Close();

          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