Sign in to follow this  
Followers 0
Johnell

Server/client chat application

9 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1353967429' post='5004331']
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
[/quote]

Could you elaborate? Not sure what you mean.
0

Share this post


Link to post
Share on other sites
You might want to run though a bit of how [url="http://www.irchelp.org/irchelp/rfc/chapter4.html#c4_1_2"]the IRC protocol[/url] 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. Edited by frob
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0