Solved How to send a message from a Tcpserver to all connected clients?
-
Hello to all. Can someone help me to find a way to send a message on Tcpserver event to all connected clients? I just want to send an string to all clients when a pushbutton on server is clicked.
-
Just remember all open sockets in a vector/map/whatever and iterate over this container when you want to send data.
-
Hi @Z0mbi3,
You can get an example of what @Christian-Ehrlicher said here: https://code.qt.io/cgit/qt/qtserialbus.git/tree/src/plugins/canbus/virtualcan/virtualcanbackend.cpp#n144
Regards
-
Allright, thanks to @Christian-Ehrlicher and @aha_1980 to solved my question.
-
and for what its worth, there is really no such thing as a TCP broadcast since it is session/stream oriented. Implementing such is purely an application level function. As others suggested, your daemon needs to keep track of the client list and insert the broadcast message into each client's stream where/when it makes sense to do so. adhoc insertion of messages needs to be thought out carefully when dealing with application level session protocols. Many protocols can break if they receive unsolicited messages from the server, as it is often a client->server query/command, followed by a server->client response.
-
@Kent-Dorfman
In which eventuality you'd like to send a out-of-band message, https://en.wikipedia.org/wiki/Out-of-band_data, or e.g. https://docs.microsoft.com/en-us/windows/desktop/winsock/protocol-independent-out-of-band-data-2. Which I have a feeling Qt doesn't support anyway.