Question

This is a chat client/server program. To make a broadcast I have to save every client that connects and then write to each one of them, am I right? What's the best practice to manage the clients?

    GNAT.Sockets.Accept_Socket(
         Server  => Receiver,
         Socket => Connection,
        Address => Client
    );
    Channel := GNAT.Sockets.Stream(Connection);
    String'Write(Channel, "Welcome Message");
Was it helpful?

Solution

I think you might start by using GNAT.Sockets.Socket_Set_Type to hold the sockets of interest, which are the server socket and all the currently connected client sockets.

Use GNAT.Sockets.Check_Selector to wait until something readable happens, then GNAT.Sockets.Get to find the socket (or one of the sockets) concerned; if it’s the server socket, then a new client has connected, otherwise one of the existing clients has input to read.

I’ve an example of this approach in my Embedded Web Server; but note this doesn’t deal with the case where one of the clients responds so slowly that writes get blocked. Recovering from that sort of situation would be more complex.

OTHER TIPS

Yes, you would have to write to each client. Although there's multicast sockets which can take care of some of that for you.

The best practice for managing clients is to let someone else do it for you.

I got tired of repeatedly writing socket code, so I simply adopted ZeroMQ and wrote TOMI_4_Ada to manage all the client/server, pub/sub minutiae.

There's also other Ada friendly protocols out there, like YAMI4, that do the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top