Hello! I am writing a program in which there is a server that waits for client connections and records the Connection List of connected clients (sockets). It is necessary for the server to then receive messages from one client and forward it to another (definite) client. te how to organize these working pairs? (client1-client2; client3-client4)

Client and server are written in Java ...

  • You need to somehow identify connections, usually it happens after login. And when sending a message to indicate in it to whom it is intended. - Vartlok

1 answer 1

Write a simple class, if you have not already done so - a UserConnection, which will contain the methods for sending and sending. It will look something like this: 1) the client (socket) sends a request to the server with information about the message: to whom it wants to send the message and the message text itself 2) the server receives a message from the client with ip1, which it wants to send to the client with ip2 and finds it in ConnectionList Connection , whose ip = ip2, and through the output stream sends the desired text.