How to make two clients communicate with each other?

This is not my homework (my homework is just communicating with a client and server that works correctly, especially with your help [:-)] but I want two clients to communicate with each other, I don’t know that when I I get the text from the first, how can I send this text to another client. Please help me .thanks.

public class MainServer {

static Socket client = null;
static ServerSocket server = null;



public static void main(String[] args) {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");

    try {
        server = new ServerSocket(5050);
    } catch (IOException ex) {
        System.out.println("Could not listen on port 5050");
        System.exit(-1);

    }
    try {
        boolean done = false;
        while (!done) {

            client = server.accept();
            System.out.println("Client Connected...");
            BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter streamOut = new PrintWriter(client.getOutputStream(),true);
            String line = streamIn.readLine();
            if (line.equalsIgnoreCase("bye")) {
                streamIn.close();
                client.close();
                server.close();
                done = true;
            } else {
                System.out.println(line);
                streamOut.println(line);
            }
        }

    } catch (IOException e) {
        System.out.println("IO Error in streams " + e);
    }
}}
+3
source share
6 answers

To do this, your two “clients” will act as both a client and a server: Listening for incoming things on a socket and sending files over other sockets.

+3
source

, . ( ServerSocket, ). , , Set, .

EDIT: - , , . -, , .

+2

, ~ 100 , -.

+2

.

, Java.

0

P2P, , , , .

, IP- . , "" .

, , . , ( ) ( ).

, . , , , , ? , , , , , , .

0

, :

  • , / (xmpp/jabber )
  • , . .

, , .

, . , / , () . .

: , .

If the client disconnects, the worker thread closes the socket, returns it to the pool, and terminates.

0
source

Source: https://habr.com/ru/post/1728934/


All Articles