You are on page 1of 8

NetGMS Tutorial

NetGMS is a base set of scripts that handles GameMaker:Studio's Networking
system in a simple to setup fashion. This tutorial will go through and give an
explanation of each script and it's use in the framework.
Net_Setup( tcp , udp );
Net_Setup() is the initiation script for the NetGMS framework, calling
this script defines whether TCP and UDP will be used or not, as well as the
variables used by both the TCP and UDP sides of the NetGMS framework.
Framework Global Variables Initiate By Script:
If using TCP: NetTcp_Sck, NetTcp_Bfr, NetTcp_Con,
NetTcp_Dis, NetTcp_Ipa,
NetTcp_Prt, NetTcp_Que,
NetTcp_Lst.
If using UDP: NetUdp_Sck, NetUdp_Hst, NetUdp_Bfr,
NetUdp_Ipa,
NetUdp_Prt, NetUdp_Map.
Script Arguments:
tcp : Set to true to use TCP or false to not use TCP.
udp : Set to true to use UDP or false to not use UDP.
Script Note:
The NetGMS framework will throw errors if you do not call the
Net_Setup
script. Calling this script should be done before
attempting to use any other
scripts within the framework.
Script Return Value:
The script returns a default value of 0.

Net_TcpServer( port , maxclients , buffer size );
Net_TcpServer() creates a TCP based server socket. The socket ID for
the TCP server socket is stored in the NetTcp_Sck globalvar.
Extra Assets Created By Script:
buffer : fixed size buffer of a user defined size. This buffer is
stored in the
NetTcp_Bfr globalvar.
ds_list : empty ds_list for storing connected client socket IDs.
This ds_list is
stored in the NetTcp_Lst globalvar.
ds_list : empty ds_list for storing client socket IDs for a client to
request. This
ds_list is stored in the NetTcp_Que
globalvar.

Script Notes: . url / ip . Stored in the NetTcp_Prt globalvar. Stored in the NetTcp_Ipa globalvar.Script Arguments: port : Port to host the server socket on. Script Note: This script uses dynamic memory. call Net_TcpDestroy() before calling the next script. Net_TcpClient() and Net_TcpClientRaw() scripts all store their socket IDs and buffer IDs inside of the NetTcp_Sck and NetTcp_Bfr globalvars. buffer size : Size to use when creating the fixed sized buffer. C++ or any other programming language you're hosting a server on. url / ip : The URL or IPv4 address the server is hosted on. The Net_TcpServer(). Stored in the NetTcp_Prt globalvar. The socket ID for the raw client connection is stored in the NetTcp_Sck globalvar. make sure to delete the memory via Net_TcpDestory() when the sockets are no longer needed. Extra Assets Created By Script: buffer : fixed size buffer of a user defined size. So attempting to call these scripts one after the other in one project will result in each socket and buffer being over written by the next call of the scripts. This buffer is stored in the NetTcp_Bfr globalvar. Net_TcpClientRaw( port . Net_TcpClientRaw() creates a raw based TCP client connection to a server application not created with GameMaker: Studio. Script Arguments: port : Port to create the raw client connection on(server port?). This will result in losing socket IDs and buffer IDs and will result in a memory leak. buffer size ). To avoid this. maxclients : Total number of clients that can connect to the server. This script can be used for connecting to a server made in C#. -1 if the server socket failed to be created. Script Return Value: 1 if the server socket was successfully created. buffer size : Size to use when creating the fixed sized buffer.

See the Net_TcpServer() script for notes for this script. Script Return Value: 1 if the raw client socket was successfully created. ds_map : ds_map for holding the IP address and server name of servers found using broadcasting. Extra Assets Created By Script: buffer : fixed size buffer of a user defined size. url / ip . The socket ID for the raw client connection is stored in the NetTcp_Sck globalvar. Net_UdpServer() creates a UDP server socket as well as a UDP client socket. The server socket receives data while the client socket sends data. Net_TcpClient() creates a TCP based client connection to a server created in GameMaker: Studio.1344 update. Stored in the NetTcp_Ipa globalvar. This buffer is stored in the NetUdp_Bfr globalvar. Stored in the NetTcp_Prt globalvar.3. Note: The client socket is no longer needed as of GMS update v1. Script Notes: See the Net_TcpServer() script for notes for this script.3. -1 if the client socket failed to be created. url / ip : The URL or IPv4 address the server is hosted on. Broadcasting is done via . The UDP scripts will be changed in the future for the v1. use the Net_TcpClientRaw() for raw client functionality. -1 if the raw client socket failed to be created. buffer size ). url / ip . Extra Assets Created By Script: buffer : fixed size buffer of a user defined size. buffer size ). Script Arguments: port : Port to create the client connection on(server port?). maxclients . buffer size : Size to use when creating the fixed sized buffer. Net_UdpServer( port . The UDP client socket is stored in NetUdp_Sck globalvar and the UDP server socket is stored in NetUdp_Hst globalvar. This buffer is stored in the NetTcp_Bfr globalvar. Net_TcpClient( port . This script cannot be used to create a raw client socket. Script Return Value: 1 if the client socket was successfully created.1344.

set to 0. Script Notes: When a client connects. stores the socket ID of each client until each . the data is passed to a custom user inputted script via "script id". This NetUdp_Map globalvar. Net_TcpDestroy(). Net_Handle() handles receiving all data from TCP and UDP sockets. The user defined script here should be used to identify packets and process those packets accordingly. make sure to delete the memory via Net_UdpDestory() when the sockets are no longer needed. Script Notes: This script uses dynamic memory. NetTcp_Lst. the client's socket ID is stored in two ds_lists. NetTcp_Lst and NetTcp_Que. ds_map is stored in the Script Arguments: port : Port to host the UDP sever on.NetUdp_Cast(). url / ip : The URL or IPv4 address to host the server on. -1 if the client and server sockets failed to be created. Stored in the NetUdp_Prt globalvar. Script Return Value: The script returns a default value of 0. Net_TcpDestroy() and Net_UdpDestroy() will delete and reset all the dynamic memory and variables used by the server and client creation scripts. Stored in the NetUdp_Ipa globalvar. buffer size and packet ID. and Net_UdpDestroy(). When data is received. Net_Handle( script id ). However. the script also handles receiving client connections and removing clients from the server as they disconnect. maxclients : UDP servers do not support client connections. The ds_List. Script Arguments: script id : User defined script that will be used to pass to and process the received data. buffer size : Size to use when creating the fixed sized buffer. The data passed in order: buffer ID. Script Return Value: 1 if the client and server sockets were successfully created.

an IP address of "192.2.145. Net_Analyze() checks the current connection of a client to a server to see if the connection is still valid. For example. Net_UdpCast( packet id . This script is a simple rename and pass through for GameMaker: Studio's buffer_tell( buffer id ) function. server name ).122'' has a subnet of "192.x" meaning that all IP addresses with "192.2. Script Return Value: The script returns a default value of 0. The ds_list. NetTcp_Que.145.client disconnects. Net_Analyze( packet id ). The subnet/ip address is that of the UDP socket created by Net_UdpServer()." in them will receive this packet. Script Return Value: 1 if the connection is still valid and -1 if the conneciton has been lost. If a .2. server name : The name(as string) of the UDP server sending the broadcast. socket Script Return Value: The script returns a default value of 0. Net_Bytes() returns the total number of bytes written to the buffer from the current seek position of the buffer. Script Arguments: packetid : The ID of the packet being broadcast. Net_Bytes( buffer id ). Script Return Value: The script returns a default value of 0. stores the ID of each client until the client requests it's socket ID via Socket Requesting. Net_Inquire() this script is a followup script to Net_UdpCast(). Net_Inquire( buffer id ).145. Net_UdpCast() creates a basic packet with a packet ID(message id/msgid) with the name of the server attached to the packet and sends(broadcasts) the packet to a range of IP addresses under the same subnet.

Script Return Value: The script returns a default value of 0. this script will send a finalization packet back to the server. Net_Inquire() will read the server name from the buffer and get the IP address of the server and store it in the NetUdp_Map ds_map with server name as the key and IP address as the value. Net_Inquire() can be used to process the packet received from the broadcast. packet id ). Net_Requeue( buffer id . this script will remove the client's socket ID from the socket request ds_lsit. Used on Client) When a socket request packet sent by the server with Net_Enqueue() is received. (2. NetTcp_Que. Used on Server) When the finalization packet sent from a client with Net_Requeue() is received on the server. -. Used on Server) Loops through all of the non-requested sockets in the NetTcp_Que ds_list on the server and sends a verification packet to each socket. Net_Enqueue( packet id ). (3.Socket Requesting Scripts -Socket requesting is a system where the server sends each client a packet. Script Return Value: The script returns a default value of 0. telling the server the client has successfully received it's requested socket ID on the server. Script Return Value: The script returns a default value of 0. (1. ------------------------------------- . Net_Dequeue( buffer id ). Script Return Value: The script returns a default value of 0. telling the client to request it's socket ID from the server as a form of identification.broadcats is received from another UDP server. This packet when received by the client with Net_Requeue() tells the client to finalize requesting it's socket.

Net_TcpAssets() returns the value of the specified asset via asset id. List of Assets: (asset IDs -> defined constants) udp_asset_sck(NetUdp_Sck) : UDP "client" socket. tcp_asset_bfr(NetTcp_Bfr) : Server / Client buffer ID. tcp_asset_prt(NetTcp_Prt) : Port the client(raw client) socket is connected on. udp_asset_prt(NetUdp_Prt) : Port the server is hosted on. udp_asset_msx(size of NetUdp_Map) : Size of list in asset udp_asset_map. udp_asset_bfr(NetUdp_Bfr) : Server buffer ID. tcp_asset_que(NetTcp_Que) : (Server) List ID of unrequested client socket IDs. tcp_asset_lsz(size of NetTcp_Lst) : (Server) Size of list in asset tcp_asset_lst. Net_UdpAssets( asset id ). udp_asset_map(NetUdp_Map) : ds_map of servers foudn with broadcasting. . Extra Constants: network_error(-1) : used for checking if a socket has an error. udp_asset_hst(NetUdp_Hst) : UDP "server" socket. tcp_asset_dis(NetTcp_Dis) : Last client socket ID to disconnect from the server. List of Assets: (asset IDs -> defined constants) tcp_asset_sck(NetTcp_Sck) : Server / Client socket ID. tcp_asset_ipa(NetTcp_Ipa) : IP address the client(raw client) socket is connected to. Net_UdpAssets() returns the value of the specified asset via asset id. tcp_asset_qsz(size of NetTcp_Que) : (Server) Size of list in asset tcp_asset_que. tcp_asset_lst(NetTcp_Lst) : (Server) List ID of client sockets connected to the server. udp_asset_ipa(NetUdp_Ipa) : URL / IP the server is hosted on.Net_TcpAssets( asset id ). network_noerror(1) : used for checking if a socket has no error. tcp_asset_con(NetTcp_Con) : Last client socket ID to connect to the server.

msg_broadcast(254) : preset packet id used for broadcasting. triggered_udp(1) : used for checking if UDP received a packet. msg_analyze(255) : preset packet ID for validing the connection to the server. triggered_tcp(0) : used for checking if TCP received a packet. .msg_queue(253) : preset packet id used for socket requesting.