This function broadcasts the data from a buffer locally to a range of IP addresses (for more information on buffers see Reference - Buffers).
The range is limited to that of the device running the server, such that if the device has an IP of 92.168.11.130, then the data will be broadcast over the range 92.168.11.*. The function will return the number of bytes of data sent, or a number less than 0 if the send has failed.
NOTE This function will only work when used with UDP - your server needs to be TCP and your client needs to have a UDP client socket created with network_create_socket_ext in order to receive any broadcasts sent from the server.
NOTE This function will not work when used in a project running on the HTML5 target.
IMPORTANT iOS 14 and later require that you request the multicast entitlement (com.apple.developer.networking.multicast) and enable it in order to send and receive multicasts and broadcasts. To make GameMaker add this entitlement you should enable broadcast networking in the iOS Game Options / tvOS Game Options (not doing this will throw an error).
Moreover, iOS 14 does not show the consent modal dialog to the user until a packet is first sent. So the alert will not be shown if the UDP socket only receives. A way to work around this is to send an empty "dummy" packet so that the consent modal is triggered.
network_send_broadcast(socket, port, bufferid, size);
Argument | Type | Description |
---|---|---|
socket | Network Socket ID | The id of the socket to use. |
port | Real | The port that the server will use. |
bufferid | Buffer | The buffer to get the data from. |
size | Real | The size (in bytes) of the data. |
buffer_seek(broadcast_buffer, buffer_seek_start, 0);
buffer_write(broadcast_buffer, buffer_string, global.ServerName);
network_send_broadcast(server, 6511, broadcast_buffer, buffer_tell(broadcast_buffer));
The above code writes the name string of the current server (stored in global.ServerName), then writes it to a buffer stored in broadcast_buffer. This data is then broadcast locally to a range of IPs (the device IP is currently implied as the broadcast base range) to port 6511.