• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Local UDP Networking without IP

Heathenlamb

Member
Hi folks .... is it possible to do basic ( I mean basic) networking at a local level with UDP, without specifying the IP address, and only the port. Or say, search for a game locally (only on the local network) via some other identifier that ISN'T the IP address ?
 

chamaeleon

Member
You can use UDP broadcast to send a message on the local network and anything listening on that port will receive it and can react. As the UDP message includes the IP of the sender, you can optionally proceed and connect back using TCP, or you can just keep using UDP, all depending on how you wish you structure your communication.
 

Heathenlamb

Member
You can use UDP broadcast to send a message on the local network and anything listening on that port will receive it and can react. As the UDP message includes the IP of the sender, you can optionally proceed and connect back using TCP, or you can just keep using UDP, all depending on how you wish you structure your communication.
Oh sweet. So I can send out the message via UDP without having to specify the IP to send it too, then obviously at the other side that are just listening for UDP messages on the same port, which will then trigger an Async event if a message comes in ?
 

chamaeleon

Member
Oh sweet. So I can send out the message via UDP without having to specify the IP to send it too, then obviously at the other side that are just listening for UDP messages on the same port, which will then trigger an Async event if a message comes in ?
Correct. Create a network_socket_udp socket, and use network_send_broadcast() to send a message. The documentation states the receiving socket must have been created with network_create_socket_ext(), in order to have a specified port it listens for your messages so it can trigger the async event (assuming both programs are implemented using GMS, be it the same project executable or not).
Edit: It's been a while since I have done this myself, so I fail to remember if it's strictly necessary to use that function to receive messages (seems to me I didn't at the time), when you can create a UDP server socket.
 

Heathenlamb

Member
Correct. Create a network_socket_udp socket, and use network_send_broadcast() to send a message. The documentation states the receiving socket must have been created with network_create_socket_ext(), in order to have a specified port it listens for your messages so it can trigger the async event (assuming both programs are implemented using GMS, be it the same project executable or not).
Edit: It's been a while since I have done this myself, so I fail to remember if it's strictly necessary to use that function to receive messages (seems to me I didn't at the time), when you can create a UDP server socket.
Perfect. Thanks for the confirmation mate. Appreciate your help and time.
 

chamaeleon

Member
Create event
GML:
global.server = network_create_server(network_socket_udp, 8888, 10);
global.client = network_create_socket(network_socket_udp);
global.received = "";
Async event
GML:
if (async_load[? "type"] == network_type_data) {
    show_debug_message(json_encode(async_load));
    global.received = buffer_read(async_load[? "buffer"], buffer_string);
}
Key Up - Enter
GML:
var buf = buffer_create(1, buffer_grow, 1);
buffer_write(buf, buffer_string, keyboard_string);
network_send_broadcast(global.client, 8888, buf, buffer_tell(buf));
buffer_delete(buf);
keyboard_string = "";
Draw event
GML:
draw_text(10, 10, "Input: " + keyboard_string);
draw_text(10, 30, "Network: " + global.received);
 

Heathenlamb

Member
Create event
GML:
global.server = network_create_server(network_socket_udp, 8888, 10);
global.client = network_create_socket(network_socket_udp);
global.received = "";
Async event
GML:
if (async_load[? "type"] == network_type_data) {
    show_debug_message(json_encode(async_load));
    global.received = buffer_read(async_load[? "buffer"], buffer_string);
}
Key Up - Enter
GML:
var buf = buffer_create(1, buffer_grow, 1);
buffer_write(buf, buffer_string, keyboard_string);
network_send_broadcast(global.client, 8888, buf, buffer_tell(buf));
buffer_delete(buf);
keyboard_string = "";
Draw event
GML:
draw_text(10, 10, "Input: " + keyboard_string);
draw_text(10, 30, "Network: " + global.received);
Oh wow ... thanks man ! This will help immensely ... the only bit I am hazy on is that still appears to have a set IP address for the server plus the client. How to I create/find the connection without the IP initially.

EDIT - Ignore that last sentence, I see what is going there now. I appreciate the code example. Makes it easy to knock out what I am trying to do. Thanks again.
 
Last edited:

chamaeleon

Member
Just a heads-up. I'm having some issues with it, and it might be related to the program not knowing which interface to send out the broadcast message on.
Code:
PS > ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : <stuff>
   IPv4 Address. . . . . . . . . . . : 192.168.1.118
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : <stuff>
   IPv4 Address. . . . . . . . . . . : 192.168.56.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
My show_debug_message() statement shows the received packet came with the second adapter IP. Two different computers do not see each other's messages currently. If it's related to this, I'm at the moment at a loss how to tell the program to use the first adapter.
 

Heathenlamb

Member
Just a heads-up. I'm having some issues with it, and it might be related to the program not knowing which interface to send out the broadcast message on.
Code:
PS > ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : <stuff>
   IPv4 Address. . . . . . . . . . . : 192.168.1.118
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : <stuff>
   IPv4 Address. . . . . . . . . . . : 192.168.56.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
My show_debug_message() statement shows the received packet came with the second adapter IP. Two different computers do not see each other's messages currently. If it's related to this, I'm at the moment at a loss how to tell the program to use the first adapter.
All good mate. I'll work on it. You have given me more than enough to work with and have gone above and beyond.
 

chamaeleon

Member
The second adapter I didn't want it to use is a VirtualBox adapter for VMs. Disabling it made my program send broadcasts using the normal LAN IP and subnet and both computers could send UDP broadcast messages to each other.
 

Heathenlamb

Member
The second adapter I didn't want it to use is a VirtualBox adapter for VMs. Disabling it made my program send broadcasts using the normal LAN IP and subnet and both computers could send UDP broadcast messages to each other.
Mate ... worked a ripper ! Just did a quick test from Windows PC to Android mini pc over LAN, via wifi. Just worked, not a problem. Takes a few secs to sync up but then it works no problem. Thanks heaps. Has made it a whole lot easier !
 
Top