Home Forums Support Connection Establish/close handler example

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #587
    Anonymous
    Inactive

    Hi, I would like to start by saying thank you, you guys have done a great job. I am pretty new at programming and so far I have gone pretty far with my first client/server program thanks to your work.

    I don’t quite understand how to use the handlers for connections that are first establish and then closed. Is there any chance I could get an example in C#.  I know I am probably asking for something super simple but I am still trying to get a grasp on programming.

    #589
    MarcF
    Keymaster

    Heya IZZO, welcome to our forums and many thanks for your interest in our network library.

    Ok, so the general idea of establish and close handlers is that you can trigger some code when a connection is established or closed.

    If we define the following two methods:

    static void ClientConnected(Connection connection)
    {
        Console.WriteLine("A new client connected - " + connection.ToString());
    }
    
    static void ClientDisconnected(Connection connection)
    {
        Console.WriteLine("A client has disconnected - " + connection.ToString());
    }

    all they do is write some text to a console window. You could perform whatever you want.

    We can then tell NetworkComms what to do with these, either on a global level, so that the methods are executed for all connections:

    NetworkComms.AppendGlobalConnectionEstablishHandler(ClientConnected);
    NetworkComms.AppendGlobalConnectionCloseHandler(ClientDisconnected);

    or on a connection specific level:

    Connection newConnection = TCPConnection.GetConnection(new ConnectionInfo("127.0.0.1", 10000));
    newConnection.AppendShutdownHandler(ClientDisconnected);

    There is no connection specific establish handler method because it would be superfluous.

    Hope that’s what you were after, if not please let me know which bit you are unsure about.

    Marc

    #783
    Anonymous
    Inactive

    Hi Developers,

    I have the following in my code.

    NetworkComms.AppendGlobalConnectionEstablishHandler(onConnection);
    NetworkComms.AppendGlobalConnectionCloseHandler(onConnectionClose);
    _tcpInfo = new ConnectionInfo(endpoint);
    client = TCPConnection.GetConnection(_tcpInfo);

    However, when the client establishes the connection with the server, the onConnection method is never fired on the client side when the connection with the server is successful.

    On the server side, I have similar code and the method fires.

    However, I need my client to know that it was successful connecting to the server and proceed.

    Any assistance would be appreciated.

    #784
    Anonymous
    Inactive

    heya gregj,

    Thanks for the post. I agree the connection establish handler should probably be called on the client side. We’ll create a bug report and try to ensure this gets fixed in the next release.

    In the mean time if the method

    client = TCPConnection.GetConnection(_tcpInfo);

    returns without exceptions you can safely assume the connection has been succesfully established. This should allow you to use the following as a temporary workaround:

    client = TCPConnection.GetConnection(_tcpInfo);
    
    //If we get to this point without an exception trigger the connection handler manually
    onConnection(client);

    Regards,

    Marc

    #785
    Anonymous
    Inactive

    Thanks Marc,

    That is basically what I’ve been doing, would just prefer a confirmation event.

     

    -Greg

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.