Click here to Skip to main content
14,326,057 members
Rate this:
Please Sign up or sign in to vote.
See more:
I create a list of connections using TcpConnectionInformation, after it I find in that list the address which used in my program by the external IP RemoteEndPoint. Which command used to close the found connection? This task is similar to the Russinovich TCPView program.

private void ClearConnection()
{
  IPGlobalProperties IPGproperties = IPGlobalProperties.GetIPGlobalProperties();
  TcpConnectionInformation[] connections = IPGproperties.GetActiveTcpConnections();
  foreach (TcpConnectionInformation tcp in connections)
  {
    if (IP_Address.Equals(tcp.RemoteEndPoint.ToString()))
    {
	// What to write to close the found connection?
    }	
  }
}


What I have tried:

I found a similar question with a solution that does not work.
private void ClearConnection()
{
  IPGlobalProperties IPGproperties =
                            IPGlobalProperties.GetIPGlobalProperties();
  TcpConnectionInformation[] connections =
                            IPGproperties.GetActiveTcpConnections();
  foreach (TcpConnectionInformation tcp in connections)
  {
    if (IP_Address.Equals(tcp.RemoteEndPoint.ToString()))
    {
      Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, 
                          ProtocolType.Tcp);
      try
      {
        mySocket.Bind(tcp.LocalEndPoint);
        mySocket.Shutdown(SocketShutdown.Both);
      }
      catch (Exception ex) {}
      finally
      {
        mySocket.Close();
      }
    }	
  }
}
Posted
Updated 4-Aug-19 4:29am
Comments
Dave Kreskowiak 3-Aug-19 14:14pm
   
If you're talking about connections that do not belong to your app but, instead, to other apps, you're asking for trouble. What you're going to end up doing is causing exceptions in or crashing those apps that are using those connections.
Gerry Schmitz 3-Aug-19 15:01pm
   
What Dave said.
Afzaal Ahmad Zeeshan 3-Aug-19 15:44pm
   
Why can't you close the connections from inside your application before it terminates?
Leonid Kazakov 3-Aug-19 16:29pm
   
I am using Telegram API
https://github.com/telegramdesktop/tdesktop

WebProxy webProxy = new WebProxy(telegramProxy, true);
TelegramBotClient Bot = new TelegramBotClient(telegramToken, webProxy);
These two command create 2 connections.

I want to disconnect from the bot. I execute the code.

Bot.StopReceiving();
Bot.SetWebhookAsync("");
Bot = null;
webProxy = null;

As a result, one connection remains. When re-executing the code.

webProxy = new WebProxy(telegramProxy, true);
Bot = new TelegramBotClient(telegramToken, webProxy);

2 connections are created again. With 3 on and off cycles, 3 incomplete connections are already created. Often, Telegam considers this a violation and prohibits connecting. To solve this problem, close the application so that the garbage collector closes all connections.
Afzaal Ahmad Zeeshan 3-Aug-19 16:41pm
   
So, leaving it on garbage collection doesn't usually works. One thing that you can do is using this inside a using block, so that application cleans up the resources before it closes.
Dave Kreskowiak 3-Aug-19 16:53pm
   
Never heard of it. Never used it and I'm not going to start now.

Therein lies the problem with using libraries off of GitHub that don't have support forums attached to them. You're essentially on your own and have to figure things out yourself, and possibly even fix bugs in the project.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

First of all visit
CurrPorts: Monitoring TCP/IP network connections on Windows
and download programm CurrPorts. It seems that C # cannot solve the problem of closing the port. We will use the utility:
private void ClearConnection()
{
  string programma = Directory.GetCurrentDirectory() + "\\cports.exe";
  string programmParam = "/close * * ";
  IPGlobalProperties IPGproperties = IPGlobalProperties.GetIPGlobalProperties();
  TcpConnectionInformation[] connections = IPGproperties.GetActiveTcpConnections();
  foreach (TcpConnectionInformation tcp in connections)
  {
    if (IP_Address.Equals(tcp.RemoteEndPoint.ToString()))
    {
      string[] parameters = tcp.RemoteEndPoint.ToString().Split(':');
      programmParam = "/close * * " + parameters[0] + " " + parameters[1];
      Process.Start(programma, programmParam);
    }
  }
}

Attention! This code must be run on behalf of the Administrator so that there are rights to delete the connection.
   
v2
Comments
Dave Kreskowiak 4-Aug-19 11:39am
   
So you're using an external utility to "force close" connection in your own app. Good luck supporting this when your app mysterious crashes.

The solution is NOT to "force close" connections. You have two possible correct solutions. The first is to study the library you're using, understand how it works, then correctly close the connections.

The second solution is to find a different library that does the same thing you need AND IS SUPPORTED when you run into problems.
Leonid Kazakov 4-Aug-19 15:45pm
   
Dear, I asked for a solution to the problem, not obvious instructions.
Dave Kreskowiak 4-Aug-19 19:27pm
   
Since I've never used the library, I can't do that.

It's very unlikely anyone else who answers questions here would know either, for the same reason.

Your BEST source of information on how to use the library is the people who wrote it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100