there is a chat server that should receive a string and send its value to all connected clients (each client is in a separate thread), but sends only to the client who sent this string, other clients do not see this message. I sort of understand that it is necessary to somehow transfer the value between the threads, but how can I do it, tell me please!
public class MyThread extends Thread{ Socket clientSocket; MyThread(Socket a) { clientSocket = a; } @Override public void run() { String remoteIp = clientSocket.getInetAddress().getHostAddress(); System.out.println("Got a connection from: " + remoteIp); System.out.println(); try { // Берем входной и выходной потоки сокета, теперь можем получать и отсылать данные клиенту. InputStream sin = clientSocket.getInputStream(); OutputStream sout = clientSocket.getOutputStream(); // Конвертируем потоки в другой тип, чтоб легче обрабатывать текстовые сообщения. DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout); String line = null; while(true) { line = in.readUTF(); // ожидаем пока клиент пришлет строку текста. System.out.println(line); out.writeUTF(line); // отсылаем клиенту строку текста. out.flush(); // заставляем поток закончить передачу данных. } } catch(Exception x) {} } and where did this thread run
public class Server { public static void main(String[] ar) throws IOException { ServerSocket ss = new ServerSocket(13372); while (true) { System.out.println("Waiting for a connection.."); Socket NewSocket = ss.accept(); MyThread NewClient = new MyThread(NewSocket); NewClient.start(); } } }