UNET NetworkTransport.AddHost - Check if port is already open
Hey everyone,
I have a question about the low level NetworkTransport interface.
I can get the server and a client to send messages to each other, but when I try to run two instances of the client the second one fails to connect, because the port is already in use by the first one.
It doesn't throw an exception, it just logs an error message in the console saying that the port is open and I don't know how to go about catching this error. I need a way to check if the port is already open before trying to connect to it
I could manually connect the second client to another port, but I would like to have that done automatically in case the player has that particular port open. Is that a reasonable thing to do or should I just throw them an error message?
In any case I need to know how to check if NetworkTransport.AddHost will fail and do something about it
Answer by Otopking · May 17, 2018 at 05:26 AM
Alright, sorry for bumping this, but I have an update in case anyone else is having the same issue :D
I figured out that if AddHost fails it will set myHostID to -1 so I ended up just checking for negative values to see if it failed.
But it still throws the 'port already occupied' error in the console,
"Cannot open socket on ip {*} and port {6969}; check please your network, most probably port has been already occupied"
which is a bit concerning, I don't feel like having a bunch of network errors in the console is the optimal way of doing things :D but at least it gets things running for the moment.
If you know any better way of going about this please tell me
Your answer
Follow this Question
Related Questions
NetworkServer dosen't realese port when i close it 2 Answers
Socket: set reusable port failed, error: Protocol not available(92), 0 Answers
Remote clients can no longer connect with new modem / router 0 Answers
How to pass argument(port) when starting a game built with Unity 0 Answers
Networking LLAPI and Port Forwarding 0 Answers