BeardedManStudios / ForgeNetworkingRemastered

See various forks, also please join the Forge Community fork of Forge Alloy! -> https://github.com/ForgeAlloyCommunity/ForgeAlloy
https://twitter.com/FarrisFaulds
Apache License 2.0
1.49k stars 306 forks source link

Improve selection of local UDP port for client #306

Open j-santander opened 5 years ago

j-santander commented 5 years ago

I'm developing a project where multiple clients will be created on the same server.

What I've found is that as I launch each client, they will be just replace the previous one in the server, because clients are tracked by IP and source port and all the clients launched share the same port.

I've noticed that the existing UDPClient.cs code tries to select one local port. However a different one is only attempted if there's an exception.

I tried to add some additional checks (based on this answer on SO: https://stackoverflow.com/a/5879681/308705) and it now works (at least for me).

Hoping it will help someone else, I'm submitting this PR.

Cheers

phalasz commented 5 years ago

Interesting. I have not ran into the issue you are fixing here although I have multiple clients running on the same machine where the server is when I'm testing.

Would you be able to provide a barebones project as well so we can validate your fix?

notjuanortiz commented 5 years ago

There should be an error thrown when selecting ports in the rare case where all ports are in use. Given there's about 65535 ports available it is rare but, it should be accounted for.