Chatroom in C / Socket programming in Linux

前端 未结 1 1606
旧时难觅i
旧时难觅i 2021-02-03 13:56

I have a simple server and client C code to do a chatroom for multiclients using threads (pthread library). The problem I\'ve been having is that I can\'t think a way of making

1条回答
  •  臣服心动
    2021-02-03 14:43

    You need a global table of all clients, protected by a mutex. When a new client connects, add them to the global table of clients. When a client disconnects, remove them from the global table.

    When a client sends a message, acquire the lock on the global table and traverse it. Send the message to all clients (other than the one that sent the message, if you don't want to echo a message back to its source).

    This is a gross oversimplification of how real servers do it. But it should be enough to get you started.

    0 讨论(0)
提交回复
热议问题