News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
How to: List all TCP connections made by your machine using C#

Sometimes you need to know what the TCP/IP connections that your machine makes across the network. This can help in detecting network problems, network bandwidth problems, and detecting malicious programs that connect to other machines or the Internet.

Using the Code:

To list all TCP connections that your machine makes across the network, follow these steps:

1.      Create a new windows application using Visual Studio 2005/2008/2010.

2.      Rename Form1 to TCPConnectionsForm.

3.      Add a RichTextBox and name it ConnectionsRichTextBox.

4.      Add a button and name it ListTcpButton.

5.      Import the System.Net and System.Net.NetworkInformation namespaces using the following statement:

using System.Net;

using System.Net.NetworkInformation;

6.      The System.Net namespace provides a simple programming interface for many of the protocols used on networks today.

7.      The System.Net.NetworkInformation namespace provides access to network traffic data, network address information, and notification of address changes for the local computer.

8.      Double click the ListTcpButton to create the click event handler for the button.

9.      Add the following code to the ListTcpButton Click Event Handler:

private void ListTcpButton_Click(object sender, EventArgse)

{

    IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

    TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();

 

    foreach (TcpConnectionInformation tcpInfo in tcpConnections)

    {

        message += "State: " + tcpInfo.State.ToString()

            + "\nLocalEndPoint: " + tcpInfo.LocalEndPoint.ToString()

            + "\nRemoteEndPoint: " + tcpInfo.RemoteEndPoint.ToString()

            + "\n\t==========================\n";

    }

 

    ConnectionsRichTextBox.Text = message + tcpConnections.Length.ToString();

}

 

 

a.      First we create an object of IPGlobalPropertiesand name it ipProperties . IPGlobalProperties class provides information about the network connectivity of the local computer. Use the static method GetIPGlobalProperties to fill ipProperties with this information.

b.     Create an array of the TcpConnectionInformationclass and name it tcpConnections. The TcpConnectionInformation provides information about the Transmission Control Protocol (TCP) connections on the local computer.

c.      Call the GetActiveTcpConnectionsmethod of the ipProperties object to get information about the Internet Protocol version 4 (IPv4) and IPv6 Transmission Control Protocol (TCP) connections on the local computer.

d.     Create a foreach statement and loop into the tcpConnections array and display information about each TCP connection.

e.     The TcpConnectionInformation class has three properties:

                                                              i.     State: Gets the state of this Transmission Control Protocol (TCP) connection.

                                                            ii.     LocalEndPoint: Gets the local endpoint of a Transmission Control Protocol (TCP) connection.

                                                           iii.     RemoteEndPoint: Gets the remote endpoint of a Transmission Control Protocol (TCP) connection.

f.       Then we display these information in the ConnectionsRichTextBox and write the count of these connections.

10.  You should review the Local endpoints ports and view your firewall settings to make sure that you allow such kinds of connection or take the necessary actions.

11.  You should review the remote endpoints to know to which IP and ports your machine connects to.

(148) Comments

Blog Message http://www.cryptoswolf.com

Really enjoyed this article.Really looking forward to read more.


Blog Message http://foodthatsings.com/foodieforum/index.php?p=/discussion/2050/super-totally-free-amazon-training-and-fantastic-results-is-what-you-can-now-get-with-us

that would be the end of this article. Here you will find some web pages that we think you will enjoy, just click the hyperlinks over


Blog Message http://cryptomunten.net

Souls in the Waves Great Morning, I just stopped in to go to your web site and thought I ad say I liked myself.


Blog Message blogs.trgauba.com/digital-slr/nikon-dslr/nikon-d3400-price-in-india-user-review-price-of-nikon-d3400-in-india-with-18-55-lens-24-megapixel-digital-camera-best-buy-nikon-d3400-with-nikon-india-warranty/

Appreciate you sharing, great article post.Thanks Again. Want more.


Blog Message http://www.seoinvancouver.com/

Very superb info can be found on website.


Blog Message http://www.seoinvancouver.com/

I value the article.Really thank you! Keep writing.


Blog Message https://soundcloud.com/user-216450098/reposts

Nice blog here! Additionally your website loads up very fast! What web host are you the usage of? Can I get your associate link to your host? I want my website loaded up as fast as yours lol


Blog Message http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567159791&profile=cmpltPrfl&profileId=1921&app=sold&searchNav=F

A round of applause for your blog post.Really thank you! Awesome.


Blog Message https://en.wikipedia.org/wiki/Lufenuron

This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your wonderful post. Also, I have shared your website in my social networks!


Blog Message http://prettymcmodel.com/

I appreciate you sharing this article post.Much thanks again.


Blog Message https://topbestbrand.com/%E0%B9%81%E0%B8%A3%E0%B8%87%E0%B8%87%E0%B8%B2%E0%B8%99%E0%B8%95%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B8%94%E0%B9%89%E0%B8%B2%E0%B8%A7/

Im grateful for the article.Really thank you! Awesome.


Blog Message https://topbestbrand.com/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B8%87%E0%B8%B2%E0%B8%99%E0%B8%9C%E0%B8%A5%E0%B8%B4%E0%B8%95%E0%B8%AA%E0%B8%9A%E0%B8%B9%E0%B9%88/

Thank you for your blog.Really looking forward to read more. Much obliged.


Blog Message http://www.indianchestsociety.com/members/apkmall/

Really appreciate you sharing this blog post.Really looking forward to read more. Much obliged.


Blog Message http://evidence4god.blogspot.com/2016/07/aslans-zealotthe-life-and-times-of.html

A round of applause for your article.Thanks Again. Cool.


Blog Message http://ow.ly/rG4k30ip0T6

The Birch of the Shadow I think there may possibly be a number of duplicates, but an exceedingly useful list! I have tweeted this. Lots of thanks for sharing!

First Previous 1 2 3 4 5 6 7 8 9 10 Next Last 
Rating
Name
Email
Comment