Currently if I start NetworkManager as a Host and if another player starts as a Host the following warning will be thrown:
“Port 19250 is likely already in use by another application. Socket was still created, but expect erroneous behavior. This condition will become a failure starting in version 2.0 of Unity Transport.”
I’d really like to let the player know that they can’t host a game because another player is already hosting. I tried manually sending dummy packets to that port using UdpClient but then unity netcode doesn’t reply back and instead throws an exception saying “invalid message header”
Is there any legit way to check if a port is in use? I’d appreciate any help
There might be another application already using that port.
If you look at the list of commonly assigned ports, you will see that the default port (7777) is the most commonly used port used for multiplayer games. If you go outside of the 7777-7788 range, then you should make sure nothing is using that port an that it isn’t blocked by a firewall or your ISP.
If you are on Windows, you can open a command prompt and check using this command:
int myPort = 7525;
bool alreadyInUse = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
.GetActiveUdpListeners()
.Any(p => p.Port == myPort);
if (alreadyInUse)
{
Console.WriteLine($"Port {myPort} is already in use.");
}
There are no integrated helper classes/methods within UTP or NGO that does this kind of thing for you, but there are plenty of ways to handle this dynamically for sure.
That code worked great!.. until I tried it on an Android device. It returns an error:
2024/08/30 13:17:09.207 26887 26951 Error Unity NotImplementedException: The method or operation is not implemented.
2024/08/30 13:17:09.207 26887 26951 Error Unity at System.Net.NetworkInformation.UnixIPGlobalProperties.GetActiveUdpListeners () [0x00000] in <00000000000000000000000000000000>:0
Is there a similar method that’s compatible with Android? Or am I missing something?