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.

(259) Comments

Blog Message http://rhizome.org/profile/amber-jones-1/

Woah! I am really digging the template/theme of this website. It as simple, yet


Blog Message http://www.casetify.com/redirect?url=http://sidneycalhoun.blogspot.com

It is pretty worth enough for me. In my view, if all


Blog Message http://www.castingzeitung.de/redir.php?url=http://josephmaley.blogspot.com

Thank you for your post.Thanks Again. Really Great.


Blog Message http://braceteeth8.thesupersuper.com/post/exactly-what-needs-to-be-included-in-a-home-inspection

Really appreciate you sharing this article.Much thanks again. Much obliged.


Blog Message http://www.directoriopr.es/buscador.php?q=%22%3E%3Ca%20href=%22http://josefcase.blogspot.com

Thanks-a-mundo for the post.Much thanks again. Want more.


Blog Message http://tvmoviesonline.com/__media__/js/netsoltrademark.php?d=harrisonwylly.blogspot.com/

Just to let you know your blog looks a little bit unusual on Firefox on my notebook with Linux.


Blog Message https://bit.ly/2M4GzqJ

Im obliged for the blog post.Really thank you! Really Cool.


Blog Message http://seo-zone.cf/story.php?title=tadalista-com

There is evidently a bundle to realize about this. I assume you made certain nice points in features also.


Blog Message http://zachery9621jv.thedeels.com/10

I value the blog article.Really looking forward to read more. Much obliged.


Blog Message http://images.google.ad/url?q=http://marianbentley.blogspot.com

Right here is the right webpage for anybody who wishes to understand this topic.


Blog Message http://www.moonfacts.com/__media__/js/netsoltrademark.php?d=kallumbravo.blogspot.com

Im obliged for the blog article.Much thanks again. Really Cool.


Blog Message http://twylafrattalifrn.contentteamonline.com/but-no-one-said-making-that-initial-investment-would-be-easy-so-before-you-home-while-yore-not-present-such-as-when-yore-on-holiday-or-the-entirety-of-a-second-home-that-you-own

Wow, great blog.Much thanks again. Want more.


Blog Message http://immeow.com/?p=217

Utterly indited written content , regards for information.


Blog Message http://megatv.kiev.ua/user/Gissevele933/

Very good post.Really thank you! Fantastic.


Blog Message http://fabriclife.org/2018/07/26/new-years-holiday-stores-hours-in-the-usa-5/

Really appreciate you sharing this blog post.Thanks Again. Awesome.

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