I am trying to create a simple chat program using Java. I have create TCP server that accepts multiple clients. Currently the clients message to server.
- How can I broadcast the message from one client to all the others?
- How can I pair of two clients. Send and recieve message to eachother?
Use a subscribe/notify mechanism
Clients must have a server socket listening as well.
UPDATE:
Your server will have some sort of registry where all your clients register for notifications.
The clients open a server socket that waits for messages.
When a client wants to send a message then it connects to the server, sends the message and the server can either send the message to all registered clients or to a specific client that is supposed to receive the message.At this part the server connects to the client’s server socket and sends the message.
Another option is not to open a server socket at the client, but the client does polling to see if the server has a message available