Continuing from my last topic, On Topic of Networking, I'll be going over the coding structure of networking.
Once again, we'll be using 39dll by 39ster. The download include the DLL, and example, and documentation.
For those not familiar with computer networking above the mplay level, connection is made to an IP or URL, whichever is playing the role of a server, and then connects to a port. When the connection is successful, data is then sent through sockets to the receiving end.
Basic Connection in 39DLL-
The first step is for the host to create a listening socket, which is used to check for clients trying to connect. In 39DLL, this is done through:
The next step is for the host to accept incoming connections using tcpaccept(listener,mode).
On the client side, the first step is to connect to a server using tcpconnect().
Sending Data-
The basic message consists of four lines of code, using 4 different functions, clearbuffer(buffer), writebyte(buffer), sendmessage(player,buffer), and whichever write*(value,buffer) function you need to store your message. Game Maker only uses one variable type, so I'll overview the system:
Memory
Bit - A 1 or a 0.
Byte - 8 bits, represents a number or value via binary.
Variable
name - values - size
Boolean - true/false - 1 bit
Short - ±32767 - 2 bytes
U Short - 0 to 65536 - 2 bytes
Integer - ±2147483647 - 4 bytes
U Integer - 0 to 4294967296 - 4 bytes
Float - A number containing a decimal - 4 bytes
Double - Can handle any number GM can - 8 bytes
Char - 1 Ascii character - 1 byte
String - A sequence of characters - length bytes
The buffer is what you actually write the data to, leave that argument blank or use 0 to set the buffer to default.
Write:
Alright, that pretty much overviews 39DLL.dll programming. Any questions?
Once again, we'll be using 39dll by 39ster. The download include the DLL, and example, and documentation.
For those not familiar with computer networking above the mplay level, connection is made to an IP or URL, whichever is playing the role of a server, and then connects to a port. When the connection is successful, data is then sent through sockets to the receiving end.
Basic Connection in 39DLL-
The first step is for the host to create a listening socket, which is used to check for clients trying to connect. In 39DLL, this is done through:
- Code:
tcplisten(port,max,mode);
port - The port to listen on.
max - The maximum clients allowed on the wait list.
mode - Set to false to block, otherwise the socket is non-blocking.
- Code:
listen=tcplisten(4444,100,1);
if (listen<=0) {
show_message("Error: "+string(listen));
game_end();
}
The next step is for the host to accept incoming connections using tcpaccept(listener,mode).
- Code:
client=tcpaccept(listen,1);
if client<=0 exit;
user[users]=client;
users+=1;
On the client side, the first step is to connect to a server using tcpconnect().
- Code:
tcpconnect(IP,port,mode);
IP - The IP or URL to connect to.
Port - The port to try to open a connection to.
Mode - Blocking is 0, non-blocking is 1, and the non-blocking+ is 2.
- Code:
tcpconnected(port);
Port - The port to check if a connection has been established.
This function returns 'true' if the connection has been made.
- Code:
<create>
server = tcpconnect("127.0.0.1",4444,2);
if (server<=0) {
show_message("Error: "+string(server));
game_end();
}
connect=false;
<step>
if !connect {
connect=tcpconnected(4444);
} else {
//networking here
}
Sending Data-
The basic message consists of four lines of code, using 4 different functions, clearbuffer(buffer), writebyte(buffer), sendmessage(player,buffer), and whichever write*(value,buffer) function you need to store your message. Game Maker only uses one variable type, so I'll overview the system:
Memory
Bit - A 1 or a 0.
Byte - 8 bits, represents a number or value via binary.
Variable
name - values - size
Boolean - true/false - 1 bit
Short - ±32767 - 2 bytes
U Short - 0 to 65536 - 2 bytes
Integer - ±2147483647 - 4 bytes
U Integer - 0 to 4294967296 - 4 bytes
Float - A number containing a decimal - 4 bytes
Double - Can handle any number GM can - 8 bytes
Char - 1 Ascii character - 1 byte
String - A sequence of characters - length bytes
The buffer is what you actually write the data to, leave that argument blank or use 0 to set the buffer to default.
Write:
- Code:
clearbuffer();
writebyte(0);
writeshort(x);
writeshort(y);
sendmessage(receiver);
- Code:
size=recievemessage(sender);
if size<=0; exit;
mid=readbyte();
switch mid {
case 0:
val=readshort();
break;
}
Alright, that pretty much overviews 39DLL.dll programming. Any questions?