monotorrent icon indicating copy to clipboard operation
monotorrent copied to clipboard

How to make sure monotorrent select correct port?

Open uponatime2019 opened this issue 4 months ago • 0 comments

Version: 3.0.2 Usually I don't change anything in EngineSettingsBuilder, but just write a torrent client running on VPS that only port 22, 3389, 90, 443 opened and seem download/upload speed too slow

So I make change in settingBuilder.ListenEndPoints to a selection port (ex 63245) and open firewall for that port, then download/upload speed go high

But on some local network, if I set port manually (but random), seem that port is closed

So i wanna check for port that ClientEngine select with default settings is open, but Engine seem don't have any prop to get selected port. Anything I can make sure my client run with optimize settings?

The answer by ChatGPT but it's not working with lib 3.0.2

// Start the engine
        await engine.StartAsync();

        // Wait for a brief moment to ensure the listener is initialized
        await Task.Delay(1000); // Delay to allow time for the listener to set up

        // Retrieve the chosen port by accessing the LocalEndpoint
        foreach (var listener in engine.Listeners)
        {
            IPEndPoint localEndPoint = listener.LocalEndPoint as IPEndPoint;
            if (localEndPoint != null)
            {
                int port = localEndPoint.Port; // Get the assigned port
                Console.WriteLine($"The chosen port is: {port}");
                break; // Exit after getting the first listener's port
            }
        }

method to check port open

public static bool IsPortOpen(int port, string host = "localhost")
{
    try
    {
        using (TcpClient client = new TcpClient())
        {
            // Try to connect to the specified host and port
            client.Connect(host, port);
            $"Port Open {host}:{port}".WriteToDebug();
            return true; // Connection succeeded, port is open
        }
    }
    catch (SocketException)
    {
        $"Port Close {host}:{port}".WriteToDebug();
        return false; // Connection failed, port is closed
    }
}

uponatime2019 avatar Oct 12 '24 16:10 uponatime2019