SOLVED [Networking] Making a not local multiplayer game

Matthias

Member
I have watched 2 tutorial series about networking. One wasn't that good, but the other was really in-depth.
So I have a basic understanding about networking.

But one thing wasn't mentioned in either of the series.
How do you make it truly online?

My project works just fine if the server and the client are both on my computer. It even works when I'm connecting with a computer that is on the same network as the server.

But (as I expected), it doesn't work with an external computer. Sadly, I read that it is hard to make a truly online game.

I have done some research and found an extension to GMS, called GMnet Engine. The problem is that it is not for 2.0.

Should I use it? Will it still work?
Or should I try to do it myself? Are there any materials on the internet for it?

If anybody has done something like this, a truly online game, how did you do it?

(I don't want to make a huge online game with matchmaking and such, I just want to play with my friends online.)


My current setup: (oversimplified)
Server:
Code:
network_create_server(network_socket_tcp, serverPort, maxClients);

Client:

Code:
client = network_create_socket(network_socket_tcp);
network_connect(client, ip, port);
I have a feeling that the ip variable is a veeery important thing to make it to online, I tried a lot of things, but the only ip that works, is the one that cmd>ipconfig>IPv4 shows, and even that only works in a local network. (I tried that external? ip too)
 

O.Stogden

Member
You need to portforward when hosting a server online. Or implement Hole Punching.

GMNet had a hole-punching feature built-in I believe, but you might need to design your own system for this now.

Aside from this, a game on an LAN works pretty much the same as a game online would function.

And yes, to connect online, you need to use your external IP, which is the IP address of your router, not your internal IP of your PC itself.

The basic premise of hole-punching is to connect to a matchmaking server as the "middle-man", and that matchmaking server will allow a host and a client to form a connection with each other, without the host having to manually open a port on his PC. Because the host is the one making the connection to the matchmaking server, that means the host itself has opened the connection, and it lets clients join through it.

Hole-punching is basically port-forwarding without your players having to manually port-forward themselves, the game will do it for them.

If you were going for a "central server" kind of game (such as an MMO), and not one where people are hosting their own servers, then you just need to open the ports of the router that the server PC is using.

 
Last edited:

Matthias

Member
@O.Stogden
1.
I made the whole system using the TCP protocol, but you have attached UDP hole-puch.
So that means I have to redo the whole thing using UDP? Or does it work with TCP too?

2.
GMNet had a hole-punching feature built-in I believe, but you might need to design your own system for this now.
Could you please specify why can't I use GMNet? It sound much simpler.
I researched a bit, but I can't really find anything about doing GMS Hole-Punch myself.
This was the best thing I could find, but this is also just an extension. (In the answer)
Do you know about any good resources?
 

O.Stogden

Member
Using an extension might simplify it for you. I only suggested not using GMNet as it's out of date. I'm not sure of what networking options are available for GMS2, as a lot of the network extensions and services for GMS1 never got updated.

Personally I've never done hole-punching in my games, I've always required port-forwarding. Hole-punching is on the slightly more advanced side of networking.

You can do TCP or UDP hole-punching I believe, however UDP is generally the accepted way you communicate in a lot of online games, due to its faster nature.

UDP is less accurate, but it can be handled/delivered faster.

TCP makes sure data is communicated properly, but because of that, it takes longer.

So for a fast-paced game, you should use UDP, if you were making something slow, such as a turn-based game, TCP might be better.
 

Matthias

Member
I only suggested not using GMNet as it's out of date.
Fair point. I too try to not use out-of-date stuff.

UDP is less accurate, but it can be handled/delivered faster.

TCP makes sure data is communicated properly, but because of that, it takes longer.
I see. I think I will use TCP because I don't need speed.

And you mentioned that you use port-forwarding instead of hole-punch.
I did some research and I found that it is not done in GMS. So just just have to set it on your router. Right?
And I think this will be good for me, because I don't need a 24/7 running server.
This was mainly the video I watched.
 

Matthias

Member
@O.Stogden
I tried what the video said. It is just normal port forwarding. And I didn't change anything in my code.
And it didn't work.

I tried connecting from the same computer to itself. From one computer to another. And even tried connecting from 4G using USB internet sharing.
I tried connecting to the external IP. (What Google and other sites say). I even tried to change which local IP to forward to, but nothing worked.
I don't know what am I doing wrong.

How did you do port forwarding? The same way as in that video? (Just going to the router settings and creating a Virtual Server under NAT)
 

O.Stogden

Member
Each router is different.

But on my router, I just login to the admin dashboard, go to the firewall rules, there is then a section called port forwarding. Then I create a TCP rule (in your case), and tell it to open the port number that you chose in GMS, and then tell it to forward that number to my PC.

1619553703018.png

For Minecraft for example, on my router, I have a rule called "Minecraft", I then choose "Desktop-C0CUGPB" as the computer to forward to, the IP address of which is 192.168.1.133 in my case. I then tell it to forward port 25565 for both TCP & UDP.

That is all that is needed for me to host a Minecraft server on my PC, as Minecraft only uses Port 25565 to host a server.
 

Matthias

Member
@O.Stogden
I think I made it work.

I did just what you did in that picture, but after you answer, I searched this specific router and on the bottom of this page, there is a picture where one specific IP address has a red box around it.
I tried connecting to that IP with an external client (Using the 4G USB internet sharing function of my phone) and it worked.
I don't know what that IP is, all I know that it works and is different from both the local router IP and the external IP (That Google shows).

The router's built-in manual says this:
1619612772433.png

The strangest thing is that it isn't the IP that Google shows.
And when I go to a site that shows my IP address location, it is 25km-s off.
Do you know what is this IP? And what the heck is going on?
 

TsukaYuriko

☄️
Forum Staff
Moderator
Without knowing the addresses in question (and please don't share your IP address publicly, for your own safety), it'll be hard to tell what it stands for. Certain IPv4 ranges are reserved for local networks, so if it starts with 192, 172 or 10, it's probably a local one. If it then ends with 0, it's likely the local network ID, and if it ends with a 1, it's the router's local IP. If it doesn't exhibit any of those characteristics, it's likely your router's external IP.

Note that the external IP of your router doesn't necessarily equal your public IP, which is the one that connects you to the internet and is also the one the internet sees you as (e.g. what Google tells you). Your router may be connected to a subnet of your ISP which then collectively connects a bunch of devices to the internet under the same IP. This is common practice in areas where IPv4 addresses are scarce as there aren't enough IPv4 addresses to assign one to every device, or even every person.


Before you try to make any kind of online game: How much control do you have over your network? For example, if you live in a college dorm where the outermost layer is controlled by an educational institution, you can be forwarding ports internally all you want and still may not be able to get any data in or out over the internet because the outermost router doesn't have that port forwarded.
 

Matthias

Member
@TsukaYuriko
Yeah, it is probably the routers external IP. It doesn't exhibit any of those characteristics.
And I think you are right with that ISP collectively connecting IPs theory too.
But the important thing is that this IP and only this IP works for external connections.

And we have our own router with no other layers and I don't plan on hosting a server, just sometimes play with my friends.
(I know about IPs changing and what not, but it won't really affect me)

So thank you @O.Stogden for helping with the solution and @TsukaYuriko for the explanation! This community is the best. :)
 
Top