question

aboulianne avatar image
aboulianne asked

Socket bind failed: already in use (Unity HLAPI custom server)

Hello,

I'm in the process of setting up a custom game server, using Unity's HLAPI.

My current problem is that, once the server instance is started, trying to bind UNet on the provided IP address and port yields an error saying that this socket is already in use.

Socket: bind failed, error: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Cannot open socket on ip {34.228.36.47} and port {9000}; check please your network, most probably port has been already occupied

Here is how I set up the server in Unity, inside my custom network manager class, which derives from

NetworkManager:

networkPort = m_hostPort; // retrieved from "-server_host_port="
serverBindAddress = m_hostDomain; // retrieved from "-server_host_domain="
serverBindToIP = true;
StartServer();

Right after this call, the socket error appears.

I haven't found any other call to StartServer that is reachable from my server code.

Have I missed something obvious but essential?

My game title is A70F

We're still on the Free plan, and I'm already through 7/20 server hours, so I have to stop trial and errors on my side for a moment until I have more answers.

I'm ok if PlayFab stats need to consume some server time to assist in the issue.

Thanks in advance!

unity3dCustom Game Servers
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

aboulianne avatar image aboulianne commented ·

I also looked up the sample unity custom server, but couldn't tell what I wasn't doing correctly compared to the sample: It uses the IP and port provided from command line without tampering it whatsoever.

0 Likes 0 ·
aboulianne avatar image aboulianne commented ·

As an added test, I tried running it on my pc:

gameserver.exe -batchmode -nographics -logFile D:\serverProject\log.txt -server_host_port=9000 -server_host_domain=127.0.0.1

The server starts without a socket error.

If I launch another instance again, using the same address and port (but different log file to avoid confusion), then I do get the error on the second running instance, as one could expect (the prior instance, still running, occupies port 9000 at that address & protocol.)

To make sure it's not a socket leak issue, I tried killing all running instances on my pc then launching one again: No socket error, so there's no dangling socket that would leech the port.

What could be hogging port 9000 on the EC2 instance?

0 Likes 0 ·
aboulianne avatar image aboulianne commented ·

I complemented my sample to call netstat -a -n -o to see if anything was listening to the port provided by the instance starter, but nothing using that port came up.

That may have to do with my process not having suffiscent priviledges to look up all the processes running inside the VM or on the machine itself... or that there is indeed nothing hogging the port, but the firewall or some policy prevents me from binding to it.

tl;dr I'm completely lost at that point.

0 Likes 0 ·

1 Answer

·
brendan avatar image
brendan answered

I can definitely say that there's nothing in EC2 which would steal that port from you. If there were, every game with hosted servers across all of PlayFab would have issues, since port 9000 is always the first one assigned on any new server host. Presumably your server exits as soon as it hits this error, yes? Can you try uploading a build that does not quit after this error? We could then try logging into one of your servers to see if we can spot what's consuming the port.

12 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

aboulianne avatar image aboulianne commented ·

After another separate test and lots of research, I managed to establish a connection by not binding the server code to an ip address (effectively listening on 0.0.0.0 or IPAddress.Any in c#).

See

https://community.playfab.com/questions/16245/binding-socket-in-custom-server.html
for the rest of the investigation.

Thanks for the help!

1 Like 1 ·
andrewalpha avatar image andrewalpha aboulianne commented ·

Thanks for this aboulianne! I was in the same situation: Unity HLAPI custom server, and unable to listen on the provided IP address (with the same errors given in your initial post). Listening on 0.0.0.0 (with the provided port) works though.

0 Likes 0 ·
aboulianne avatar image aboulianne commented ·

Hi, thanks for your help.

The server actually doesn't quit after hitting the error yet. The program remains running "indefinitely" (i.e. I always just terminate them manually for now. I wanted to test core functionnality before adding fallbacks.)

The build currently available can be used to test. It will output contents from netstat as well as some other debug traces in the log, which could be of use.

0 Likes 0 ·
brendan avatar image brendan aboulianne commented ·

Thanks, I pulled a copy over to my own test Title ID to check. Logging onto the machine directly, and once the game server instance was running, there was nothing showing up on port 9000 - including the game server instance. But there was also no log file anywhere - there was a folder created in the output files directory, but there wasn't anything in it. I also checked throughout the exe's folder and sub-folders, and didn't see any log files there. How are you getting the details on the socket binding error?

0 Likes 0 ·
aboulianne avatar image aboulianne brendan commented ·

Hello,

Just to verify, what command line args did you use to launch it? When trying on my PC, here is the command line:

gameserver.exe -batchmode -nographics -server_host_port=9000 -server_host_domain=127.0.0.1 -logFile D:\project\gameserver\log.txt

(The arguments format isn't uniform yet in my demo. I also use -logFile

<log_file_path> instead of Playfab's original -log_file_path=

<log_file_path> which allows me to leverage Unity's Debug.Log calls instead of creating another log system.)

if gameserver isn't launched with -batchmode -nographics then it will start in game mode instead, which won't try to open the port.

I will try a simpler method to try and isolate the issue, by starting the server from scratch in a bare minimum unity project.

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.