user3530012
user3530012

Reputation: 740

Client to client messaging in socket programming

I'm working on a project which is a server-client application based on WinSock in C++. I've done whatever needed for communicate between the server and the client that I am able to send and receive messages between them.

Now I need to know how to accomplish the client-to-client messaging part and how the work must be done. I only know that for instance if Client A and Client B need to communicate with each other, the client A must send the message to the server, server receives the message and send it to client B.

If this is the proper approach please help me to know how the server should know anything of of the receiver (which is the Client B in this case)?

Please let me know if I haven't explained clearly my problem.

Any help is appreciated.

Upvotes: 2

Views: 4337

Answers (3)

Ozeta
Ozeta

Reputation: 329

Think the client-server communication as an old Radio communication.

  1. A needs to talk with B, so B must be listening
  2. A finishes his speech (you just send a keyword, something like '//EOC', or use a fixed width for messages ).
  3. B needs to talk with A, so A must not be talking, to let B talk to A

iterate until end-of-communication command

so just imagine that talk is write, and listen is read

 B:  read  -> A:  write 
 A:  read  -> B:  write 

remember to use a keyword to disconnect the 2 clients, or you could have some bad behaviour from the system calls

Upvotes: 1

Hasan Manzak
Hasan Manzak

Reputation: 663

One way to accomplish this is have one of the client to act as a server after some communication over actual server. You need to create a protocol of yours. That being said:

1- Client A: -Hey, Server!. Tell the Client B to create a comm server so I can communicate directly with him.

2- Server: -Hey, Client B!. Start a server instance and tell me the connection properties so i can relay it Client A, so he can connect to you.

3- Client B: -Hey, Server!. I'm ready to accept the connection request for Client A. Here's my connection properties...

4- Server: -Client A!! Here the connection properties of Client B. Take it or leave it... I'm done..

5- Client A: -Hey, Server B!.. Can i connect?..

That is the protocol.. So any client first initiates a request to server that includes the message type of "me, requesting to connect to client x..". Server commands the client x, client x responses, server relays the response to initiator client... And also you should implement the error handlings, denial policies or some other things you can think of in order to manage the whole protocol.

Upvotes: 7

LawfulEvil
LawfulEvil

Reputation: 2337

  1. Client A talks to server.
  2. Client B talks to server.
  3. Server determines A needs to talk directly to B and vice versa.
  4. Server sends A and B a message which contains details of how to talk to each other (IP address, ports, etc). Message also details which (A or B) will initiate contact, timeouts, etc.
  5. Upon receipt, each A and B start listening on ports detailed by server.
  6. Whoever was setup in the message to initiate contact, does so (A talks to B or vice versa).

Unless I totally missed the point of the question. You need to define a couple new messages with all the details needed for A and B to talk and send it to both A and B and they need to receive/process the message and react accordingly.

Upvotes: 3

Related Questions