GML GMS2: Networking Client to Host issue

I

i1der

Guest
Hello,

As an introduction, I am working on just a simple networking game using the Game Maker Studio 2 with GML. Ever hear of the game slay? I thought it would be a great idea to get back into the groove of game maker.

Its been probably 5 days since starting, and 3 days since I've run into this issue. I have been googling and searching and trying any suggestion or thought I could. The issue comes when I try to send a packet to the server. The server sees a client connect, and the server can send packets to the client just fine. But the server will not, for the life of me, receive anything from the client.

Let me show you what I have:

obj_server:
Code:
server_port  = get_string( "Port to listen to?", "8078" );

network_set_config( network_config_use_non_blocking_socket, true );

server_socket = network_create_server( network_socket_tcp, real(server_port), 32);

if( server_socket < 0 )
{
    show_error( "Server could not listen to port: " + server_port + ". Please try a different port, or opening up used port.", false );
    game_restart();
}

// connection established

socket_list = ds_list_create();

// declare our packet id variables
PING = 0; // ping the host

Code:
var packet_id = ds_map_find_value( async_load, "id" );

if( packet_id == server_socket )
{
    var packet_type   = ds_map_find_value( async_load, "type"   );
 
    switch( packet_type )
    {
        case network_type_connect:
        {
            var client_socket = ds_map_find_value( async_load, "socket" );
         
            ds_list_add( socket_list, client_socket );
         
            break;
        }
     
        case network_type_disconnect:
        {
            var client_socket = ds_map_find_value( async_load, "socket" );
         
            ds_list_delete( socket_list, client_socket );
         
            break;
        }
     
        case network_type_data:
        {
            var packet_buffer = ds_map_find_value( async_load, "buffer" );
         
            var packet_key    = buffer_read( packet_buffer, buffer_u16    );

            switch( packet_key )
            {
                case PING:
                {
                    // recieved ping back, display message
                    var packet_buffer = ds_map_find_value( async_load, "buffer" );
                 
                    // extract our message
                    var buffer_data   = buffer_read( packet_buffer, buffer_string );
                 
                    // display the message recieved
                    show_message( buffer_data );
                 
                    break;
                }
             
            }
         
            break;
         
        }
    }
}

Code:
// send ping
// tell the client that this is a ping with a greeting
var buffer_key  = PING;
var buffer_data = "From host: Hello!";
        
var send_buffer = buffer_create(256, buffer_grow, 1);

buffer_seek ( send_buffer, buffer_seek_start, 0           );
buffer_write( send_buffer, buffer_u16       , buffer_key  );
buffer_write( send_buffer, buffer_string    , buffer_data );

// send off our ping packet to all clients

for( var i = 0; i < ds_list_size( socket_list ); i++ )
{
    network_send_packet( ds_list_find_value( socket_list, i ), send_buffer, buffer_tell(send_buffer) );
}

// clear memory
buffer_delete( send_buffer );

obj_client:
Code:
server_ip   = get_string( "Host IP Adress?", "127.0.0.1" );
server_port = get_string( "Host Port?", "8078" );

network_set_config( network_config_use_non_blocking_socket, false );

client_socket = network_create_socket(network_socket_tcp);

if( client_socket < 0)
{
    show_message( "Error creating client socket!" );
    game_restart();
}

server = network_connect(client_socket , server_ip, real(server_port));

if( server < 0 )
{
    show_message( "Connection to the Host could not be made." );
    game_restart();
}

// declare our packet id variables
PING = 0; // ping the host

Code:
var packet_id = ds_map_find_value( async_load, "id" );

if( packet_id == client_socket )
{
 
    var packet_buffer = ds_map_find_value( async_load, "buffer" );

    // key for decifering the packet data
    var packet_key    = buffer_read( packet_buffer, buffer_u16    );

    switch( packet_key )
    {
        case PING:
        {
            // we got a ping from the host
            var packet_buffer = ds_map_find_value( async_load, "buffer" );
                 
            // extract our message
            var buffer_data   = buffer_read( packet_buffer, buffer_string );
                 
            // display the message recieved
            show_message( buffer_data );
             
            break;
        }
    }
}

Code:
// ping the host with a greeting
var buffer_key  = PING;
var buffer_data = "From client: Hi!";
                
var send_buffer = buffer_create(256, buffer_grow, 1);

// setup our buffer and add our key to it
buffer_seek ( send_buffer, buffer_seek_start, 0           );
buffer_write( send_buffer, buffer_u16       , buffer_key  );
buffer_write( send_buffer, buffer_string    , buffer_data );

// this is the problem? host does not recieve this.
network_send_packet( client_socket, send_buffer, buffer_tell(send_buffer) );

// clear up memory
buffer_delete( send_buffer );

Does any of this look wrong? Or is there an error between the keyboard and the chair? Any suggestions or ideas would be amazing!


Thank you,

i1der

Solution by: FrostyCat
When you receive data as the server, the async_id[? "id"] is NEVER the server socket's ID, instead it is always an ID given by async_id[? "socket"] from a connect-type Networking event earlier on. Take the server's data-receiving code out of the packet_id == server_socket check.
obj_server:
Code:
var packet_id     = ds_map_find_value( async_load, "id" );
var client_socket = ds_map_find_value( async_load, "socket" );

if( packet_id == server_socket )
{
    var packet_type   = ds_map_find_value( async_load, "type"   );
   
    switch( packet_type )
    {
        case network_type_connect:
        {
            var client_socket = ds_map_find_value( async_load, "socket" );
           
            ds_list_add( socket_list, client_socket );
           
            break;
        }
       
        case network_type_disconnect:
        {
            var client_socket = ds_map_find_value( async_load, "socket" );
           
            ds_list_delete( socket_list, client_socket );
           
            break;
        }
    }
}
else
{
    var packet_buffer = ds_map_find_value( async_load, "buffer" );
   
    if( !is_undefined( packet_buffer ) )
    {
        var packet_key    = buffer_read( packet_buffer, buffer_u16    );

        switch( packet_key )
        {
            case PING:
            {
                // recieved ping back, display message
                var packet_buffer = ds_map_find_value( async_load, "buffer" );
                   
                // extract our message
                var buffer_data   = buffer_read( packet_buffer, buffer_string );
                   
                // display the message recieved
                show_message( buffer_data );
                   
                break;
            }
               
        }
    }
}
 
Last edited by a moderator:

FrostyCat

Member
When you receive data as the server, the async_id[? "id"] is NEVER the server socket's ID, instead it is always an ID given by async_id[? "socket"] from a connect-type Networking event earlier on. Take the server's data-receiving code out of the packet_id == server_socket check.
 
I

i1der

Guest
Hello FrostyCat,

Thank you! Your solution solved my problem. I have been learning the new networking from https://help.yoyogames.com/hc/en-us/articles/216754698-Networking-Overview

Inside I think they really confused me:
Code:
var n_id = ds_map_find_value(async_load, "id");
if n_id == server_socket
    {
    var t = ds_map_find_value(async_load, "type");
    switch(t)
        {
        case network_type_connect:
            var sock = ds_map_find_value(async_load, "socket");
            ds_list_add(socketlist, sock);
            break;
        case network_type_disconnect:
            var sock = ds_map_find_value(async_load, "socket");
            ds_map_delete(socketlist, sock);
            break;
        case network_type_data:
            //Data handling here...
            break;
        }
    }
Especially with the section:
Code:
 case network_type_data:
            //Data handling here...
            break;
Once again, thanks for helping! I have been slamming my head on my desk just figuring out why this wouldn't work.


Thank you!

i1der
 
Top