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.

(30) Comments

Blog Message https://pronostic.pro

Major thanks for the blog.Thanks Again. Much obliged.


Blog Message http://www.ic.gc.ca/app/ccc/srch/nvgt.do;jsessionid=0001yNKRkfg7edS0wgVc3SfcF3F:-C807D?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567159791&profile=cmpltPrfl&profileId=1941&app=sold&searchNav=F

Thanks for sharing, this is a fantastic blog.Really thank you! Fantastic.


Blog Message http://www.kitchens-forsale.co.uk

This site really has all of the information I needed about this subject and didn at know who to ask.


Blog Message https://youtu.be/uQDTcXEq4I4

You have made some decent points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this site.


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

dF5IxV Really appreciate you sharing this post.Really looking forward to read more. Cool.


Blog Message Pharmg816

Hello!


Blog Message Pharme32

Hello! viagra india generic http://via3indian.com/#4.html


Blog Message Pharmb549

Hello! [url=http://via3indian.com/#2.html]viagra india generic[/url]


Blog Message Pharmd664

Hello!


Blog Message Pharmf294

Hello! viagra india http://via3indian.com/#4.html


Blog Message Pharmd361

Hello! [url=http://via3indian.com/#2.html]viagra india[/url]


Blog Message Pharmb890

Hello!


Blog Message Pharmg147

Hello! generic viagra india pharmacy http://via3indian.com/#4.html


Blog Message Pharmg579

Hello! [url=http://via3indian.com/#2.html]generic viagra india pharmacy[/url]


Blog Message Pharmd874

Hello! [url=http://via3indian.com/#2.html]viagra india[/url]

First Previous 1 2 Next Last 
Rating
Name
Email
Comment