DarkRiftNetworking / Hazel-Networking

Hazel Networking is a low level networking library for C# providing connection orientated, message based communication via TCP, UDP and RUDP.
http://www.darkriftnetworking.com
MIT License
206 stars 44 forks source link

Get port when binding to the free port #13

Open xmedeko opened 6 years ago

xmedeko commented 6 years ago

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.

JamJar00 commented 6 years ago

Hard to say if this is actually a bug or if it's good behaviour but I'd probably say it should return the actual port as you expect.

I don't have much time to spend on this project anymore (most of my time is spent on DarkRift 2) but I'd gladly merge a pull request that fixes this 🙂