0
\$\begingroup\$

I'm trying to make a simple multiplayer experience with Game Maker, using a local network, and the basic idea is to use Game Maker's built-in networking functions.

Using a simple client-server pattern, I want to create a server only if there is no active server in my local network. My idea is to scan all the IPs from 192.168.0.0 to 192.168.255.255 (inelegant, I know, I'll figure out a way to determine "192.168.x.x" after solving this problem, maybe looking at the subnet mask). Here is the code:

var n1 = 192;
var n2 = 168;
var n3 = 0;
var n4 = 0;
var server = -1;
var ip = "";

while(n3 <= 255 && server < 0){

    ip = string(n1) + "." + 
         string(n2) + "." + 
         string(n3) + "." + 
         string(n4);
    show_debug_message("Trying " + ip);
    server = network_connect(global.socket , ip, port );

    n4 += 1;
    if(n4 > 255) {
        n4 = 1;
        n3 += 1;
    } 
}
if(server >= 0)
    show_debug_message("Found! Here you are: " + ip);
else
    show_debug_message("No IP found, man!");

And that's the problem: the first IP doesn't work, the game freezes for a minute and then all the others tries will fail, even the right one, with this error:

trying 192.168.255.254

ioctlsocket failed with error: -1

Error (0x 2736): Could not set socket option

My tries:

  1. I tried starting with the right IP and it works, so, the connection works.

  2. I tried lowering the timeout to one second and the effect is what I expected: after one second the first attempt fails and all the latter fail as well

Any idea to check the connection before actually try it? Thanks in advance!

\$\endgroup\$
1
\$\begingroup\$

Before answering, I tested your code in order to get your error, but it took too much time because of networking functions inside the loop - from 192.168.0.0 to 192.168.0.98 it took 25 minutes. This is an unacceptable waste of time, in my own opinion.

I have a solution to your issue though, which involves a slight deeper knowledge of networking: broadcasting.

Since you want to know if there are any servers in the local network, you can just send broadcast packets by using the GML function network_send_broadcast(). A broadcast packet is a special packet whose destination is a range of IP addresses; if the server address is 192.168.10.207, it will broadcast to addresses 192.168.10.*, so any device in the same subnet and using the right port will receive the incoming broadcast packet.

What is this for? You can use it for multiple purposes, for example getting the list of all servers currently running in the local network (many games let this for LAN games). For your purposes, I can think of two solutions.

Servers send beacons to clients

Your server uses a socket to send periodical broadcast packets, which can be received from clients when players are looking for running servers. Then, clients can connect to the current server, otherwise (e.g. a timer runs out) there are no running servers so they start a new game as server themselves. If you allow multiple servers in the same network, broadcast packets will have different sender IP addresses, and you'll have to implement sort of server list on your own.

Clients look for servers

On the other hand, we have clients sending broadcast messages looking for servers. When a server receives such packet, replies back with its IP address, allowing a client to connect to it. As well as before, after the client fires a "Hello, is anybody here?" packet(s) without getting any response within a certain amount of time, it can decide to set up and start a new game as server itself.

Both cases allow clients to be aware of multiple servers presence, if needed. In my own opinion, the latter solution is nicer because unnecessary network traffic is avoided.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much, I didn't even think about broadcasts =) Now every client asks to everyone through a broadcast if someone is a server. If not, he becomes the server and everyone else will become its client. I'll update my question with the working code! \$\endgroup\$ – Simone Chelo Oct 25 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.