Jump to content

  • Log In with Google      Sign In   
  • Create Account


Server/client chat application


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Johnell   Members   -  Reputation: 352

Like
0Likes
Like

Posted 26 November 2012 - 01:01 PM

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.

Sponsor:

#2 hplus0603   Moderators   -  Reputation: 4899

Like
0Likes
Like

Posted 26 November 2012 - 02:02 PM

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 };

#3 Johnell   Members   -  Reputation: 352

Like
0Likes
Like

Posted 26 November 2012 - 02:25 PM

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.

#4 frob   Moderators   -  Reputation: 18367

Like
0Likes
Like

Posted 26 November 2012 - 04:03 PM

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
Check out my personal indie blog at bryanwagstaff.com.

#5 Johnell   Members   -  Reputation: 352

Like
0Likes
Like

Posted 26 November 2012 - 04:24 PM

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.

#6 frob   Moderators   -  Reputation: 18367

Like
0Likes
Like

Posted 26 November 2012 - 05:05 PM

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.

Edited by frob, 26 November 2012 - 05:06 PM.

Check out my personal indie blog at bryanwagstaff.com.

#7 Johnell   Members   -  Reputation: 352

Like
0Likes
Like

Posted 27 November 2012 - 04:47 PM

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.

#8 frob   Moderators   -  Reputation: 18367

Like
0Likes
Like

Posted 27 November 2012 - 05:14 PM

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.
Check out my personal indie blog at bryanwagstaff.com.

#9 Johnell   Members   -  Reputation: 352

Like
0Likes
Like

Posted 27 November 2012 - 05:31 PM

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?

#10 frob   Moderators   -  Reputation: 18367

Like
0Likes
Like

Posted 27 November 2012 - 05:50 PM

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.
Check out my personal indie blog at bryanwagstaff.com.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS