• 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!

GML Node.JS Server Receiving "Random" Data Inside Of A Buffer

WanSou

Member
Hello, I am trying to make a server in Node.JS.
But whenever I sent a buffer from the game maker client
the Node.JS server is receiving "Random" data and the real data
and I don't really know :
-if its the issue with Node.JS or Game Maker
-what is this additional data that the server is receiving
-how can I "remove" the additional data so I am left over with the data that I originally sent

in the screenshot below
you can see the data that the Node.JS server is receiving

out of these 17 random numbers
4 of those are the actual data that I've sent from the game maker client
and the rest is just... random to me

cmd_5yMlr0D41V.png

I am really new to Node.JS
and fairly new to networking
so don't expect me to understand some... advanced stuff...
 

chamaeleon

Member
Both code and stating which numbers are valid and which ones are not would be ... helpful? In the meantime, I assume you're using the _raw suffix functions when sending data to a non-GMS server.
 

WanSou

Member
Both code and stating which numbers are valid and which ones are not would be ... helpful? In the meantime, I assume you're using the _raw suffix functions when sending data to a non-GMS server.
Ah, sorry, forgot about that
i tried the _raw suffix and the _packet just incase, both output random numbers

Red = those numbers are the data that i've sent
Orange = one of these numbers are the data that i've sent... ( i sent a 0 but i have no idea which one is the actual one )

ApplicationFrameHost_C5UHfLpeQC.png
And i should have specified that the screenshot uses the
GML:
network_send_packet()
since _raw outputted some even weirder numbers

my bad, i should have given more information
here is the _raw with the code that sends the buffer
the data from the buffer is not even closely fammiliar to the one that i've sent

GameMakerStudio_cTbejUUrhP.png

and here is the... Node.JS code
good luck with reading it... I learnt node.js like a day ago
and 99% of this code isn't mine

JavaScript:
var net = require("net");
var port = 62621;

var server = net.createServer();
let clients = []


server.on("connection",function(socket)
{
    var addr = socket.remoteAddress;
    console.log("New Connection: " + addr);
    console.log("Socket: ", socket);
    let clientAdd = clients.push(socket)

    socket.on("data",function(data)
    { 
        for(var i = 0; i < data.length-1; i++) {
            console.log(data.readUInt16BE(i))
        }

    });

    socket.once("closed",function()
    {
        console.log("Player Disconnected: " + addr);
    });
});

server.listen(port,function()
{
    console.log("The Server has been Started");
});

i might be just missing something
i am really new to both networking and node.js
so, sorry if there is a really obvious mistake on my part...

tell me if i forgot/if you want more information

edit :
i did notice that when i swap the line 18 with "console.log(data.readUInt8(i))" the outputs are different, it outputs
123
65

still not the original values i've sent (except the first one)
 
Last edited:

WanSou

Member
I don't know why but... my message is awaiting moderators approval before going public
1600712197801.png
i just feel like saying it so it doesn't feel like i'm ghosting you...
i guess i just have to wait ?

edit : it has been approved...

Both code and stating which numbers are valid and which ones are not would be ... helpful? In the meantime, I assume you're using the _raw suffix functions when sending data to a non-GMS server.
 
Last edited:

chamaeleon

Member
network_send_packet() manual page
Packets sent with this function are formatted such that the GameMaker Studio 2 game receiving the data can "split" the packets correctly
So don't use that with a nodejs server, use network_send_raw() (extra data is sent and expected by GMS if you don't).

network_connect()
The connection uses a special protocol that ensures only GameMaker Studio 2 games connect to each other
So use network_connect_raw() (extra data sent and expected by GMS if you don't).

Your nodejs code appears to read 2 bytes per loop iteration which does not correspond to storing single bytes per item.
 

WanSou

Member
Yeah i am using the network_connect_raw() to connect to the server, without it i couldn't even connect...

and... i dont think i understood the "reading 2 bytes per loop" part ?
i also found a mistake in the node.js code, i accidently put " i < data.length-1 " in the for loop, which was skipping 1 of the bytes
with that fixed, it now is recieiving the values of

123
65
176

still not the values that i have sent in the test buffer from gms
it is "reading" every byte, but it is reading it incorrectly

could you... speak to me like a 5 year old ?
i dont have much experience with networking and node.js
 

WanSou

Member
I'm assuming
Code:
console.log(data.readUInt16BE(i))
reads 2 bytes (16 bits, big endian) per iteration over data.length.
Oh Right...
i tried it with "console.log(data.readUInt8(i))"
(i should have mention that my bad...)
but it outputs the numbers that i have put in the other response
1600716555434.png
aka, not the numbers that i have put in the buffer inside of gms
(i also have no idea whats "big endian")
 

chamaeleon

Member
You could try the following and see if it makes a difference.

Create event
GML:
network_set_config(network_config_use_non_blocking_socket, 1);
connected = false;
connecting = false;
sent = false;
Step event
GML:
if (!connected && !connecting) {
    show_debug_message("Connecting to server");
    socket = network_create_socket(network_socket_tcp);
    network_connect_raw(socket, "127.0.0.1", 62621);
    connecting = true;
}

if (connected && !sent) {
    show_debug_message("Sending data to server");
    buf = buffer_create(1, buffer_grow, 1);
    for (var i = 0; i < 20; i++) {
        buffer_write(buf, buffer_u8, 10+i*10);
    }
    network_send_raw(socket, buf, buffer_tell(buf)+1);
    buffer_delete(buf);
    sent = true;
}
Async network event
GML:
show_debug_message("Received network message");
show_debug_message(json_encode(async_load));

var type = async_load[? "type"];
switch (type) {
    case network_type_connect:
        show_debug_message("Connected to server");
        connected = true;
        connecting = false;
        break;
   
    case network_type_disconnect:
        show_debug_message("Disconnected from server");
        connected = false;
        break;
   
    case network_type_data:
        show_debug_message("Received data from server");
        break;
   
    case network_type_non_blocking_connect:
        show_debug_message("Non-blocking connect");
        connected = true;
        connecting = false;
        break;
   
    default:
        show_debug_message("Unexpected network type [" + string(type) + "]");
        break;
}
GMS program output
Code:
Connecting to server
Received network message
{ "port": 62621.000000, "socket": 0.000000, "succeeded": 1.000000, "id": 0.000000, "ip": "127.0.0.1", "type": 4.000000 }
Non-blocking connect
Sending data to server
Node output
Code:
[...] socket output [...]
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
Edit: Forgot to mention that sending values over 255 as single bytes won't exactly give you the value you expect (looking at you, 321 and 432...)
 
Last edited:

WanSou

Member
It did output this

1600721805062.png
right when the gms client started

sorry for the late reply...

edit :
just saw your edit
so it is working ?
why is there a 0 at the end of the output?
 

chamaeleon

Member
The reason I used buffer_tell(buf)+1 instead of buffer_tell(buf) is because my output only went to 190 without the addition. It seems to me that I should not need to do it and my original code did not have it, but with 190 as the last value I was a bit confused. I'm no less confused with your output having an additional 0 at the end that I do not receive (which seems more correct than my output despite being garbage data, but I can't explain the difference).
 

chamaeleon

Member
Regarding your original node code, if you wish you use 16 bit numbers (and given adding number using buffer_u16), you might need to make a change like this
JavaScript:
...
        for(var i = 0; i < data.length/2; i++) {
            console.log(data.readUInt16LE(i*2))
        }
...
Of course, data.length/2 and i*2 only works if all you send are 2 byte integers. If you start mixing and matching, you'll need to keep more explicit track of the offset you look at in data.
 

WanSou

Member
Regarding your original node code, if you wish you use 16 bit numbers (and given adding number using buffer_u16), you might need to make a change like this
JavaScript:
...
        for(var i = 0; i < data.length/2; i++) {
            console.log(data.readUInt16LE(i*2))
        }
...
Of course, data.length/2 and i*2 only works if all you send are 2 byte integers. If you start mixing and matching, you'll need to keep more explicit track of the offset you look at in data.
Alright thank you for the help <3
 
good luck with reading it... I learnt node.js like a day ago
and 99% of this code isn't mine
You shouldn't be copying other people's code especially when starting out. You need to learn Node (Learn javascript first) with patience by using the manual, reading articles, googling, watching youtube videos, udemy courses, etc. You won't learn it in a day or a week. Learning to program is a journey, not a destination. You need to actually put in the effort.

Learn with baby steps and only ask the GMC if you have exhausted all your options and you absolutely can't figure it out. Otherwise, you won't develop the skill of problem solving which programming is heavily based upon and you will always be relying on others to do everything for you.

Edit: I apologize if this came off as mean. These are just facts. I didn't know how to sugar coat it, lol.
 
Last edited:
Top