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.

(345) Comments

Blog Message http://kaseylandry.soup.io/

Muchos Gracias for your post.Really thank you! Cool.


Blog Message http://www.duo.no/

What Is The Best Way To Import MySpace Blogs To Facebook?


Blog Message https://www.goodreads.com/user/show/86704846-whitney-morgan

Perfect work you have done, this internet site is really cool with superb info.


Blog Message http://b3.zcubes.com/v.aspx?mid=1094210

There as certainly a lot to learn about this issue. I love all the points you have made.


Blog Message https://www.pornofilmpjes.com

Thank you for your post.Really thank you! Fantastic.


Blog Message http://vinochok-dnz17.in.ua/user/LamTauttBlilt452/

we prefer to honor quite a few other net web-sites on the net, even though they aren


Blog Message http://humour-france.com/modules.php?name=Your_Account&op=userinfo&username=BudMather4

when it comes when it comes to tv fashion shows, i really love Project Runway because it shows some new talents in the fashion industry**


Blog Message https://www.creativehomeidea.com/clean-up-debris-only-with-the-best-company/

I think other web site proprietors should take this site as an model, very clean and fantastic user genial style and design, as well as the content. You are an expert in this topic!


Blog Message https://orcid.org/0000-0002-8489-1896

Very neat blog article.Really looking forward to read more. Want more.


Blog Message http://anlun.com/__media__/js/netsoltrademark.php?d=simplelinksbacks.xyz%2Fstory.php%3Ftitle%3Dvisit-website-116&

LANCEL SACS A MAIN ??????30????????????????5??????????????? | ????????


Blog Message http://goldberg-audit.ru/bitrix/redirect.php?event1=&event2=&event3=&goto=http://twitter.com/garrynorton6/status/1128239414482821120

This web site is mostly a stroll-through for the entire info you needed about this and didn


Blog Message https://www.ttosite.com/

Know who is writing about bag and also the actual reason why you ought to be afraid.


Blog Message https://www.youtube.com/watch?v=gdIX4g3NB9Q

Thank you ever so for you article.Thanks Again. Keep writing.


Blog Message https://www.youtube.com/watch?v=gdIX4g3NB9Q

Thank you ever so for you article.Thanks Again. Keep writing.


Blog Message http://elgg.hycloud.co.il/blog/view/14630/the-purposes-of-a-commercial-freezer-or-fridge

You have a very good layout for your blog i want it to use on my web page too

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