1

Recently, I started playing around with Game Maker to make a very simple game with the ability to play online (multiplayer). I made a very simple client & server. I can send data from the server to the client, but I can't send the data from the client to the server.

Client: (Create event)

client_socket = network_create_socket(network_socket_tcp);
var server = network_connect(client_socket, "127.0.0.1", 5200);
if(server < 0) show_message("Could not connect! Try turning on the server?");
else{ //Send string
    var t_buffer = buffer_create(256, buffer_grow, 1);
    buffer_seek(t_buffer, buffer_seek_start, 0);
    buffer_write(t_buffer , buffer_string, "Hello");
    network_send_packet(client_socket, t_buffer, buffer_tell(t_buffer));
    buffer_delete(t_buffer);
}

Server: (Create event)

server_socket = network_create_server(network_socket_tcp, 5200, 5);

(Async Network event)

var n_id = ds_map_find_value(async_load, "id");
if(n_id == server_socket){
    var t = ds_map_find_value(async_load, "type");
    socketlist = ds_list_create();
    if(t == network_type_connect){
        var sock = ds_map_find_value(async_load, "socket");
        ds_list_add(socketlist, sock);
    }
    if(t == network_type_data){
        var t_buffer = ds_map_find_value(async_load, "buffer"); 
        var cmd_type = buffer_read(t_buffer, buffer_string);
        show_message(cmd_type);
    }
    //show_message("Something happened!");
}

For some reason, the async network event in the server is never triggered when the client sends data. The message Something happened! only comes up when the client either connects or disconnects, but not when data is sent. Using almost the exact same code, I can send from the server, but not vice versa. Is this a problem with the code or just a server/client limitation?

Rob
  • 4,927
  • 4
  • 26
  • 41
Avadonia
  • 45
  • 8

1 Answers1

2

This is the server side 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");
    socketlist = ds_list_create();
    if(t == network_type_connect){
        sock = ds_map_find_value(async_load, "socket");
        ds_list_add(socketlist, sock);
    }
}

if(n_id == sock) {
    var t_buffer = ds_map_find_value(async_load, "buffer"); 
    var cmd_type = buffer_read(t_buffer, buffer_string);
    show_message(cmd_type);
}

You have to use the socket id when a message is arriving. The network_type_data will never be triggered.

Also, you have to declare the sock variable in the server's create event with a negative number (like noone (-4)).

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
T-bond
  • 93
  • 2
  • 11