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.

(5) Comments

Blog Message Delphine

Hi, my name is Delphine and I am the sales manager at SwingSEO Solutions. I was just looking at your Ultra Developers - How to: List all TCP connections made by your machine using C# website and see that your site has the potential to become very popular. I just want to tell you, In case you didn't already know... There is a website network which already has more than 16 million users, and the majority of the users are interested in niches like yours. By getting your site on this network you have a chance to get your site more visitors than you can imagine. It is free to sign up and you can find out more about it here: http://zoy.bz/4nm - Now, let me ask you... Do you need your site to be successful to maintain your way of life? Do you need targeted traffic who are interested in the services and products you offer? Are looking for exposure, to increase sales, and to quickly develop awareness for your website? If your answer is YES, you can achieve these things only if you get your site on the service I am talking about. This traffic service advertises you to thousands, while also giving you a chance to test the service before paying anything at all. All the popular sites are using this network to boost their traffic and ad revenue! Why aren’t you? And what is better than traffic? It’s recurring traffic! That's how running a successful website works... Here's to your success! Read more here: http://zoy.bz/4nm - or to unsubscribe please go here: http://innovad.ws/8h9dp


Blog Message Brigitte

Hi, my name is Brigitte and I am the marketing manager at SwingSEO Solutions. I was just looking at your Ultra Developers - How to: List all TCP connections made by your machine using C# site and see that your site has the potential to become very popular. I just want to tell you, In case you didn't already know... There is a website service which already has more than 16 million users, and most of the users are interested in niches like yours. By getting your site on this service you have a chance to get your site more popular than you can imagine. It is free to sign up and you can find out more about it here: http://qa.juststicky.com/yourls/278u - Now, let me ask you... Do you need your website to be successful to maintain your business? Do you need targeted visitors who are interested in the services and products you offer? Are looking for exposure, to increase sales, and to quickly develop awareness for your website? If your answer is YES, you can achieve these things only if you get your site on the network I am describing. This traffic network advertises you to thousands, while also giving you a chance to test the network before paying anything at all. All the popular sites are using this service to boost their readership and ad revenue! Why aren’t you? And what is better than traffic? It’s recurring traffic! That's how running a successful website works... Here's to your success! Find out more here: http://claimyourexcellence.info/24o - or to unsubscribe please go here: http://todochiapas.mx/C/36p


Blog Message Air Jordan 6 Retro World Cup Brazil

Cheap 2495 Our have [url="https://www.youtube.com/watch?v=eJZIvKridus"]Original Air Jordan Sneakers[/url] Air Jordan 6 Retro World Cup Brazil https://plus.google.com/110031452100752742978/posts/VXn5VQFYkoq/


Blog Message Jetsin

Ah, i see. Well that's not too tirkcy at all!"


Blog Message Filipe

There's any way to know what is the pid of the application that owns those connections?

Rating
Name
Email
Comment