Skip to content

Get port when binding to the free port #13

@xmedeko

Description

@xmedeko

I want to crate a TcpConnectionListener to bind to the first free port. It works fine, however, I cannot get the port number:

listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Loopback, 0));
int port = ((IPEndPoint)listener.EndPoint).Port; // 0
// reflection hack
var socket = ((Socket)listener.GetType().GetField("listener", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(listener));
port = ((IPEndPoint)socket.LocalEndPoint).Port; // 63930 - the real port number

Note: I want to use this for a Unity - other local process IPC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions