GML [SOLVED] Networking - Server send to connected clients

Discussion in 'Programming' started by robeng, Apr 7, 2018.

  1. robeng

    robeng Member

    Joined:
    Sep 3, 2017
    Posts:
    7
    I'm currently exploring networking and I'm creating a chat program for 2 users as a first project. I have set up everything I need except one thing. I want the server to send to my two clients a text string, the string with the chat history. My server has two variables names socket1 and socket2. Can I use these socket variables to send data to the clients or do I need to save their IP addresses?

    I'm using TCP and my sockets are saved as socket1 and socket2.

    This is what I have treid thus far. From my Async oServer event i send a packet to the Client. The client have a async event that should recive a string of the entire chat. But, when trying, the client does not recive any packets, and I'm not sure that the server even sends!

    oServer Async event:
    Code:
    /// @description Check for clients and data
    var type_event = async_load[? "type"];
    
    switch (type_event) {
        case network_type_connect:
            //Add the clients to the socket variable
            if (socket1 == noone) {
                scoket1 = async_load[? "socket"];
            }
            else if (socket2 == noone) {
                socket2 = async_load[? "socket"];
            }
            break;
        case network_type_disconnect:
            //Remove the clients from the socket variable
            socket1 = noone;
            socket2 = noone;
            break;
        case network_type_data:
            //Handle the data
            var buffer = async_load[? "buffer"];
            buffer_seek(buffer, buffer_seek_start, 0);
          
            //Handle data function ^^
            var messageName = buffer_read(buffer, buffer_string);
            var messageText = buffer_read(buffer, buffer_string);
            text += (messageName + ": " + messageText + "\n");
            oText.text = text;
          
            //Send packet to clients
            buffer = buffer_create(1024, buffer_grow, 1);
          
            buffer_write(buffer, buffer_string, text);
            network_send_packet(socket1, buffer, buffer_tell(buffer));
          
            break;
    }
    
    
    oClient Async event:
    Code:
    /// @description Check for server data
    var type_event = async_load[? "type"];
    
    switch (type_event) {
        case network_type_data:
            //Handle the data
            var buff = async_load[? "buffer"];
            buffer_seek(buff, buffer_seek_start, 0);
          
            //Handle data function ^^
            var messageText = buffer_read(buff, buffer_string);
            oText.text = messageText;
          
            break;
    }
    
    
    But, it won't work! Any help appreciated!
     
    Last edited: Apr 7, 2018
  2. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,521
    Check if:
    Code:
    var send_success=network_send_packet(socket1, buffer, buffer_tell(buffer));
    
    send_success is more than 0 to make sure it worked.

    Also, a typo: (whish is probably the error)
    Code:
    scoket1 = async_load[? "socket"];
    
    "scoket1" should be "socket1"
     
  3. robeng

    robeng Member

    Joined:
    Sep 3, 2017
    Posts:
    7
    Thank you! That was the problem!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice