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.

(312) Comments

Blog Message http://102711.net/__media__/js/netsoltrademark.php?d=www.juegosdemariobros.tv%2Fuprofile.php%3FUID%3D924663

You are my role designs. Many thanks to the post


Blog Message https://www.talktopaul.com/alhambra-real-estate/

Utterly pent articles , thankyou for entropy.


Blog Message https://www.suba.me/

E6uacn Im obliged for the blog post.Really looking forward to read more. Want more.


Blog Message http://buddy0362ls.wpfreeblogs.com/mix-them-to-add-accent-colons-to-your-birthday-decorations-from-0-5-to-1-25-inches-in-diameter

Really enjoyed this article. Keep writing.


Blog Message http://bgtopsport.com/user/arerapexign162/

Im obliged for the blog post. Fantastic.


Blog Message http://nibiruworld.net/user/qualfolyporry982/

Muchos Gracias for your article.Really looking forward to read more. Will read on


Blog Message https://ks-barcode.com

I simply could not depart your web site before suggesting that I extremely enjoyed the usual information an individual provide for your guests? Is gonna be again frequently to inspect new posts


Blog Message http://www.lovelesshorror.com/horrors/blog/view/199841/cleaning-services-which-could-keep-you-time-and-effort

Thanks-a-mundo for the article post.Really looking forward to read more. Cool.


Blog Message https://ks-barcode.com/barcode-scanner/zebra

Your style is so unique compared to other people I ave read stuff from. I appreciate you for posting when you have the opportunity, Guess I will just book mark this web site.


Blog Message https://redirect.camfrog.com/redirect/?url=http://docdro.id/6gl2clQ

Usually I do not learn post on blogs, but I would like to say that this write-up very forced me to check out and do it! Your writing style has been amazed me. Thanks, quite nice post.


Blog Message https://lesdefinautes.com/moncompte/login.aspx?returnurl=http://www.slideserve.com/ZaynabLeonard/123moviesnewsite-powerpoint-ppt-presentation

It`s really a nice and helpful piece of info. I am happy that you just shared this useful info with us. Please keep us up to date like this. Thanks for sharing.


Blog Message http://www.facebook-danger.fr/userinfo.php?uid=731180

It as nearly impossible to find experienced people about this topic, however, you sound like you know what you are talking about! Thanks


Blog Message http://perchsmoke10.jigsy.com/entries/general/The-Importance-of-Event-Invitations

This is the type of information I ave long been in search of. Thanks for posting this information.


Blog Message http://french6631in.sojournals.com/nine-out-of-10-people-over-age-65-receive-social-security-and-those-benefits-account-if-taken-prior-to-age-59-a-10-federal-tax-penalty-may-apply

There is certainly a lot to know about this issue. I like all of the points you have made.


Blog Message http://opalclumpneruww.tubablogs.com/any-innovation-in-agriculture-is-of-interest-i-find-agrohub-interesting-an-dam-intrigued-name-because-cdfis-are-one-of-the-best-kept-secrets-for-local-investing

Thanks pertaining to discussing the following superb written content on your site. I ran into it on the search engines. I will check back again if you publish extra aricles.

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