• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

My two player server seems to send packets to itself

I'm trying to make an all-in-one server and i'm pretty new to the networking features. What it's supposed to happen is that I send a packet from the client to the server and the server sends it back to all the clients. The problem is that the game enters an infinite loop when the server sends the packet back (i think it might be because the packet that the server sent is being caught again by the server, which generates another packet and so on, but it could be another reason).
The strangest thing is that it works fine until the client starts sending data. (And yes, I tried the client data send alone and it works fine)

Here's my code inside the obj server:

Code:
CREATE EVENT:
var port=65000
var max_client=2
server_socket = network_create_server(network_socket_tcp, port, max_client);
if(server_socket<0){
    show_message_async("Room failed to create")
    with(con_client) instance_destroy()
    instance_destroy()
}
else{
    server_buffer=buffer_create(2048,buffer_fixed,1)
    socket_list=ds_list_create()
}

NETWORKING EVENT:
    var event = ds_map_find_value(async_load, "type");          //get the type of network event
    switch(event){
        case network_type_connect:
            var socket = ds_map_find_value(async_load, "socket"); //get the socket id and store it in a variable
            ds_list_add(socket_list, socket);                 //then write it to a ds_list for future reference
            alarm[0]=10
            break;
        case network_type_disconnect:
            var socket = ds_map_find_value(async_load, "socket");
            show_message_async(string(socket)+ " disconnected")
            ds_list_delete(socket_list,ds_list_find_index(socket_list,socket));
            break;
        case network_type_data:
            var buffer=ds_list_find_value(async_load,"buffer")
            var socket=ds_list_find_value(async_load,"id")
            buffer_seek(buffer,buffer_seek_start,0)
            received_packet(buffer,socket)
            break;
    }
And this is the code inside the "received_packet" script:
Code:
var buffer=argument0
var socket=argument1
var msgid=buffer_read(buffer,buffer_u8)

switch(msgid){
    case network.state_c:
    var state=buffer_read(buffer, buffer_u8)
    switch(state){
        case 3:
            var angle=buffer_read(buffer, buffer_u8)
            var vel=buffer_read(buffer, buffer_s8)
            for(var i=0;i<ds_list_size(socket_list);i++){
                var _sock=ds_list_find_value(socket_list,i)
                buffer_seek(server_buffer,buffer_seek_start,0)
                buffer_write(server_buffer,buffer_u8,network.state_c)
                buffer_write(server_buffer,buffer_u8,state)
                buffer_write(server_buffer,buffer_u8,angle)
                buffer_write(server_buffer,buffer_s8,vel)
                network_send_packet(_sock,server_buffer,buffer_tell(server_buffer))
            }
            break;
        default: for(var i=0;i<ds_list_size(socket_list);i++){
                buffer_seek(server_buffer,buffer_seek_start,0)
                buffer_write(server_buffer,buffer_u8,network.state_c)
                buffer_write(server_buffer,buffer_u8,state)
                network_send_packet(_sock,server_buffer,buffer_tell(server_buffer))
            }
            break;
    }
    break;
}
Can anyone help me find why this is not working properly?
 
Last edited:
Ok. Updated the code. I think I revised all the variables. I always forget about putting var before local variables šŸ˜…. But still not working.
 
Top