0

I have implemented a simple client/server app using sockets, but now i would like to do so i have a ClientA that writes to server, and the server redirects the message to ClientB.

ClientA -> Server -> ClientB

I know how to implement ClientA and ClientB, but im having problems to distinguish ClientA from ClientB inside the server...

Server: I know how to read and resend the messages, i just need the logic to distinguish the clients.

4
  • I'm not sure I understand the question. You have two clients connected on two sockets and want to read from one and write to the other. The sockets have metadata that identifies the client (IP, port). What else do you need? Commented Dec 3, 2013 at 17:39
  • i need some kind of flag, to know if im the receiver or the sender. because with ip i never know who i am. Commented Dec 3, 2013 at 17:43
  • Which client is doing the sending and which is doing the receiving is totally up to you to establish by some means when the clients connect to the server. You have to make, and keep track of, that decision based on your specific application. This is not a question that can be answered by someone else. Commented Dec 3, 2013 at 17:48
  • I understand that it is up to me how do i want to implement, i just dont know how to get both connections, and then find out who is who. Could you post a simple code just showing how do you define who is who in server? thanks in advance! Commented Dec 3, 2013 at 17:55

1 Answer 1

1

If I understand the question, you have a server to which clients connect.

A server can have one of two roles, either the "sender" or "receiver". When a sender and a receiver connect to the server, the sender transmits data which is then passed on to the receiver. This is generically known as a "proxy".

One way to do this is to have the server listen on two different ports, say 3000 and 4000. Clients connecting to port 3000 (for instance) want to assume the role of sender, while those connecting on 4000 want to receive. If you have multiple senders and multiple receivers, then the clients will need to identify themselves to the server and indicate to which receiver they want to send or receive from (by sending login parameters, for instance), prior to setting up the data transfer connections. The details of how this is accomplished (data packets sent) is known as the "protocol", and you are responsible for designing it.

If clients can take on both roles simultaneously (sender and receiver) then you would have a single listening port on the server for all clients. The clients would then have to communicate to the server (by sending data packets) what connection they want to establish. Again, the details of how this happens are totally up to you. You must define the protocol.

Here's a sequence diagram of one (of many) ways to do this:

Client A          Server           Client B
   |----login------>|                 |
   |                |<------login-----|
   |                |-------accept--->|
   |<---acccept-----|                 |
   |----data------->|                 |
   |                |-------data----->|
   .                .                 .
   .                .                 .
   .                .                 .

Client A login data message says "I am client A, I wish to send data to B"

Client B login data message says "I am client B, I wish to receive from A"

Server sends "accept" messages to both. When A receives the accept message it begins sending data and the server forwards it to B.

Issues to be dealt with include ordering of connections (what if B connects before A), connection failure (how does the server notify one client that the other disappeared), etc. These are all part of defining the protocol.

Sign up to request clarification or add additional context in comments.

1 Comment

i think i got it! Yesterday i drew exactly the same sequence diagram. I dont hve much knowledge about networks, sockets, connections.. This was a good help to clarify everything. Thanks @Jim Garrison

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.