Server/client chat application

Started by
8 comments, last by frob 11 years, 4 months ago
I recently started learning about socket programming in C/C++. I've done some basic stuff, e.g. client writes something to server and it's then echoed back to the client. Now i've come to the point where i'd like to make a chat application, using these concepts. The idea i'm having is that clients can connect to different chat rooms on the server and communicate with each other. In the chat room you should be able to target another user and they should be able to talk with each other.

Now to the problem. I'm not sure which way is the easiest/best way to implement this. For the chat room I thought of when a user writes something, the message is sent to the server and the server then echoes that message to the other clients. Not sure what other options I have. Also some ideas on how to implement the private messaging between clients in a chat room would be great.

I am using C++ and unix sockets.
Advertisement
You could download some IRC daemons and read the source to them, as they implement that functionality. Unfortunately, they also do a bunch of other things, so the code might be bigger than ideal for just learning.
enum Bool { True, False, FileNotFound };
Any other ideas on what approach I should take in order to implement the functionality I want? I'd like to do most of the stuff myself, since it's a learning experience.
Instead of directly echoing the data you receive, you'll need to process it as headers and payloads.

You then have headers saying join channel, part channel, ping, set nickname, and so on.

join channelname
part channelname
ping username
MsgFrom channelname textOfMessage

Instead of directly echoing the data you receive, you'll need to process it as headers and payloads.

You then have headers saying join channel, part channel, ping, set nickname, and so on.

join channelname
part channelname
ping username
MsgFrom channelname textOfMessage


Could you elaborate? Not sure what you mean.
You might want to run though a bit of how the IRC protocol works.

Let's run through a quick IRC-like system.

You connect.

You want to log in, so you send:
SEND: NICK frob
You get back: OK


You want to join a chat room named #foo:
SEND: JOIN #foo
You get back: RPL_TOPIC #foo This is the topic of Channel #foo

You also want to join a chat room named #bar:
SEND: JOIN #bar
You get back: RPL_TOPIC #bar This is the topic for Channel #bar

Suddenly you get a message:
:#bar RobotAtBar Welcome to channel #bar! Visit our web site for the rules.
That tells you which channel it is from, which user it is from, and the message to display.

Now you want to send a message to channel #foo
Send: #foo Hi everybody!
to which you may get this response: #foo frob Hi everybody!


And so on.
I don't really feel like we're on the same page.

I wanna know how the server and clients should communicate. For example, if 3 clients are connected to a chat room and client 1 types something, how is client 2 and 3 supposed to know what client 1 wrote? Does client 1 write the message to the server which then forwards that message to client 2 and 3? Or what approach is the best for this kinda situation? This is what i'm confused about.
Yes, it goes to the server then out to the other clients.

It is a star architecture.

When someone in a chat room sends a message to the server, the server needs to identify those in the room and send the message out to each of them.
Ok, great. Now to the second problem.

Say 3 clients is connected to a chat room. Client 1 want to be able to only talk with client 2. Do the messages between client 1 and 2 also go through the server, or is a separate connection between client 1 and 2 established?
Chat servers are generally a star, not a mesh.

In a star, clients do not talk directly to each other.

Clients talk to the server, the server relays messages to all other clients.

This topic is closed to new replies.

Advertisement