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.

(285) Comments

Blog Message http://recipes-news.online/story.php?id=4443

wonderful. I really like what you have obtained right here, certainly like what


Blog Message http://best-clothing.pro/story.php?id=19556

Wow! Thank you topiew! I always wanted to write on my website something like that. Can I implement a portion of your post to my site?


Blog Message http://www.caozhiping.com/member.asp?action=view&memName=JeramyDanglow49

There may be noticeably a bundle to know about this. I assume you made sure nice points in options also.


Blog Message https://uaedesertsafari.com/vip-desert-safari.html

We need to build frameworks and funding mechanisms.


Blog Message http://amountcouch8.bravesites.com/entries/general/benefits-of-making-photographs-at-a-studio

Way cool! Some very valid points! I appreciate you writing this article and the rest of the website is also really good.


Blog Message https://hydractives.com/go.php?url=http://138sbrandvip.com/forum/profile.php?section=personality&id=1485

Very interesting subject, thanks for putting up.


Blog Message http://all4webs.com/kitequiver3/nibdgqsrzb908.htm

Very good post! We will be linking to this particularly great content on our website. Keep up the good writing.


Blog Message http://contourmattress.com/__media__/js/netsoltrademark.php?d=www.studiognata.it%2Findex.php%3Foption%3Dcom_k2%26view%3Ditemlist%26task%3Duser%26id%3D747769

Major thanks for the article.Really looking forward to read more. Really Cool.


Blog Message http://www.fmnokia.net/user/TactDrierie910/

wonderful post, very informative. I wonder why the other specialists of this sector don at notice this. You must continue your writing. I am confident, you have a huge readers a base already!


Blog Message http://forum.onlinefootballmanager.fr/member.php?1511970-Eretlearejesk745

Nonetheless, I am definitely pleased I came across


Blog Message https://bomberdeer33hagenbagge713.shutterfly.com/21

Thanks for the help in this question, I too consider, that the easier, the better


Blog Message http://linkcopper34.ebook-123.com/post/several-fundamental-ideas-for-investing-in-real-estate

Major thanks for the article.Really thank you! Want more.


Blog Message https://cablephone4.webgarden.cz/rubriky/cablephone4-s-blog/getting-real-estate-for-novices

Just Browsing While I was browsing yesterday I saw a excellent article about


Blog Message http://greatestits.com/__media__/js/netsoltrademark.php?d=wanelo.co%2Fzoneshield2

We stumbled over here by a different page and thought I might check things out. I like what I see so now i am following you. Look forward to looking at your web page for a second time.


Blog Message http://edwardchild9.drupalo.org/post/the-need-for-outstanding-customer-service

Would you be eager about exchanging links?

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