mas-bandwidth/yojimbo

How to make a server listen on localhost and internet ?

K3rhos opened this issue · 16 comments

Hello everyone, here is my issue, I'm trying to make a server for a video game project so I tested my server in localhost and everything is working but when I want a friend to connect to my server that's not working ! Just to be clear I opened the server port on my internet box !

So my server code is like that:

if (!InitializeYojimbo())
{
	printf("Failed to initialize 'Yojimbo'\n");

	return EXIT_FAILURE;
}

yojimbo_log_level(YOJIMBO_LOG_LEVEL_INFO);

yojimbo::Address serverAddress("127.0.0.1", 4674); // <---- Server ip & port to listen.

GameConnectionConfig config;

GameServer gameServer(serverAddress, config);

gameServer.Run();

When I have 127.0.0.1 in server address I can connect with my client in localhost without any issues but when I put my internet box IP my friend can connect but I can no longer on my side !

I don't really understand why we need to put an IP Address to create the server, why just not only a port, and the server listen for all IP (Localhost & Internet at same time !).

Sorry in advance for my english, I'm not a native english speaker.

Thx for your help !

Bind to "0.0.0.0" instead of "127.0.0.1"

Yeah i already tried "0.0.0.0" but that's not work for both, I can't even more connect using 127.0.0.1 and my friend can't too using my box IP.

Ah in this case your friend cannot cannot connect because you are behind NAT (network address translation).

Try hosting your server on a machine with a public IP address, like bare metal or google cloud, etc. -- Good luck!

The problem is that I want my game to be easy to mods on server side, so I want mods developers to be able to test their server locally, there is no way to make this possible with your library ?

Because in an old C# project I had succeeded to get it work with a local server via TcpListener and by using IPAddress.Any, we was able (me & my friends) to connect to the server (Localhost & Internet).

I'm sorry, it is generally not possible to do this, unless you have a public IP address, or you use NAT punchthrough.

https://www.comptia.org/content/guides/what-is-network-address-translation

So even if I host on an other computer in my house through my internet box, I would not be able to connect to the server with my computer which is on the same network ?

Sorry for all the questions, I'm a total beginner with networking and I don't really understand all the concepts for the time being.

No worries. If you bind to 0.0.0.0 and connect to your server via it's local LAN address, you should have no problem connecting to it from another computer on the same local network. -- cheers

I think the issue is here, "0.0.0.0" don't work when I use it, any machines can't connect to the server (home computer & external internet computer). Maybe a code issue but I don't think I misconfigured something, any ideas ?

OK. Work out what the server IP address is on your local network, and pass that in instead of 0.0.0.0

My bad, it's been a long time since I worked on yojimbo, and I thought you were passing in the bind address, but it's actually the address to advertise for connection.

Pass this local IP of the server address in, and on the client connect to the local server IP address, and it will work.

cheers

Okay thx for your help, I tested with my local machine and it work but my friend is actually offline now (I'm from France and it's 1:00AM) so I can't test this morning, I'll keep you informed if it work for both !

OK no worries. Remember, he has to be on the same LAN for this to work, eg. both computers need to be under the same router -- cheers

Wait... so if my friend is with a distant computer (Internet) that will not work ?

Yep. If your friend is on a distant computer, you need to do NAT punchthrough. That's complex, and yojimbo doesn't do that for you. Yojimbo is designed for client/server games where you host the server on a computer with a public IP address, like google cloud, or a bare metal data center.

cheers

Oh okay so for my case you advise me to use an other net library ? I heard about several, but don't really know what's the best for my usage.

Me neither. Sorry!

I was also confused by 0.0.0.0 not working to bind to ANY. I had it working fine with Enet, but yojimbo was unable to connect. It seems like yojimbo encodes the string version of the server address in the connect token, so despite 0.0.0.0 working fine to open a UDP port, the connect token check fails when the client tries to connect via the actual IP address. Listening explicitly on the IP address will resolve the problem.

E.g. 127.0.0.1 will work if specified on both sides
192.168.1.1 will work if specified on both sides
94.184.19.491 will work if specified on both sides

but if you listen on 0.0.0.0 or 192.168.1.1 on the server and try to connect to 94.184.19.491 from the client, the connect token will not match and the connection will fail.

This is a bit annoying and inconvenient, as is the fact that neither yojimbo nor netcode seem to have a helper function wrapping gethostbyname() or getaddrinfo() to resolve domain names to IP addresses (Enet does, and it seems like this would be useful even in a production environment, no?), but alas, it does work if you finagle it.

(note: the public IP above is purposely out of range of a valid IP address to avoid any shenanigans)