I need to implement a multithreaded broadcast chat server in C ++ (for a school project). I can not use async socket and I am writing it in C ++ under Win32 (boost based solution will be ok).
My design should be fairly simple and straightforward: the "main" thread is waiting for an incoming connection, and when a new connection arrives, it passes it to the (new) thread in the thread pool (outdated API) to read messages from the client.
This raises the first problem: (A) should the socket be transferred to another (new) thread for writing to the client? Or (B) should I use the same read-write stream? Or (C) should a unique stream be used for writing to all clients? (again, synchronously).
If I come to solution A or B when the message comes from the client, it must be somehow sent to all other connected clients: should I put it in one (shared) variable and use some synchronization construct to have the whole chain waiting his? I think it should be something like a read / write + barrier (dynamic size), but I failed.
On the other end, if I go to C solution, I need to somehow sort through the collection (for example st::list) of the "socket" or the collection of the "subscribing object" or something like that. In this case, my main concern is how to manage the collection cancellation (and, of course, its parallel reading). A possible solution would be to lock the entire collection using the same mutex .... but I don't think it will work well.
I don’t want you, of course, to solve the problem for me, but if you can tell me some template that can fit in my problem or some solution to a similar problem, that would be great. (In any case, the main problem here is the broadcast!)
thank
source
share