Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make sure monotorrent select correct port? #701

Open
uponatime2019 opened this issue Oct 12, 2024 · 0 comments
Open

How to make sure monotorrent select correct port? #701

uponatime2019 opened this issue Oct 12, 2024 · 0 comments

Comments

@uponatime2019
Copy link

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
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant