Multi threaded chat server

Hi,

I am planning to build a chat server application in linux. Im new to threads and sockets but I've read some docs on the internet about it.

What I'm trying to make is a chat server with multiple threads. Each thread handles one more more connections.

The problem I have is that I don't know what the best way is to ipc between those threads. I have read about pipes, message queue's etc.

In pseudo code my server will work like this:

main thread
1
2
3
4
5
6
7
8
wait for incoming connection, or data from a thread.
if data from thread { 
     send data to other threads
}else{
     accept connection
     start new thread
}
loop


worker thread
1
2
3
4
5
6
7
8
9
wait for incoming data from socket or main thread

if data came from main
{
     send to socket
}else{
     send data to main
}
loop


Can someone tell me what the best way is to implement a way of ipc here?

Depends on how you want your clients to communicate.
Is it a group chat or an individual chat or a mix?
I have a look at the boost library for networking. This will help you a lot, and it has some good code examples.
It is going to be a mixed chat where people can chat in rooms but they can also chat individual.

I understand the stuff about sockets, so that's not the problem. My problem is that I dont know what the best way is to send messages from thread 1 to thread 2.

Is it a good idea to do this with pipes? Because a pipe is a file descriptor i can used it together with select(); so that the thread can wait for data from the client, or data from the pipe.

thanks for the reply's.
I can think of quite a few ways of doing this,
1: simple global variables.
2: of course pipes...
3: message queues
4: managerial class

of the four options for you I would probably have a managerial class.
Depending on synchronous or async sockets..

Your individual threads then only have to receive, and send commands to your managing class, such as:

1
2
3
4
send_to_username()
send_to_chatroom()
enter_room()
leave_room()


etc. etc. etc.
Why are you creating this chat server? What are your goals?
Topic archived. No new replies allowed.