There is a Server with many connections, and the Client , which should simply correspond with each other.
Everything comes to the Server , but not to the Client . What is the problem?
Class implementing Server :
public class ServerChat { public static Thread StartServerT = new Thread(new StartServer()); public static Thread ServerSOutT = new Thread(new ServerSOut()); public static Thread ExitT = new Thread(new Exit()); public static LinkedList<DataInputStream> LLin = new LinkedList<DataInputStream>(); public static LinkedList<DataOutputStream> LLout = new LinkedList<DataOutputStream>(); public static LinkedList<Socket> LLS = new LinkedList<Socket>(); public static ServerSocket ss; public static boolean check = true; public static boolean ChekIn = false; public static int port = 6666; public static int num = 0; public static int in = 0; public static String line = null; public static void main(String[] args) { try { System.out.println("Server start work"); ss = new ServerSocket(port); StartServerT.setPriority(6); StartServerT.start(); ExitT.setPriority(8); ExitT.start(); } catch (IOException e) { System.out.println("Server not run"); } } public static class StartServer implements Runnable { @Override public void run() { try { System.out.println("Client connect run"); while (true) { // ждем подключения от клиентов Socket socket = ss.accept(); LLS.add(socket); if (socket.isConnected()) { // Для каждого клиента создаём DataInputStream и DataOutputStream OutWriteSocket(); System.out.println("Client connect " + socket.getInetAddress()); } } } catch (IOException e) { System.out.println("StartServer error"); } } public void OutWriteSocket() { try { InputStream inIS = (LLS.get(num)).getInputStream(); OutputStream outIS = (LLS.get(num)).getOutputStream(); DataInputStream ind = new DataInputStream(inIS); DataOutputStream outd = new DataOutputStream(outIS); LLin.add(ind); // Записываем всё в LinkedList LLout.add(outd); num = num + 1; System.out.println("Number of client " + in); in = in + 1; // Новый клиентский Thread для для приёма данных Thread ServerSInT = new Thread(new ServerSIn()); ServerSInT.start(); // Запускаем его if ( ! ServerSOutT.isAlive()) { ServerSOutT.start(); // Запускаем Thread для вывода } } catch (IOException e) { System.out.println("OutWriteSocket error"); } } } public static class ServerSIn implements Runnable { // Thread ввода @Override public void run() { try { System.out.println("ServerSIn run"); int w = in - 1; while (true) { line = LLin.get(w).readUTF(); // Принимаем данные // При значении ChekIn == true Thread вывода начинает // проверять значение line и отправлять его ChekIn = true; System.out.println(line); } } catch (IOException e) { System.out.println("ServerSIn error"); } } } public static class ServerSOut implements Runnable { @Override public void run() { try { System.out.println("ServerSOut run"); while (true) { if (ChekIn) { System.out.println(line); for (int i = 0; i < LLout.size(); i++) { System.out.println(line); LLout.get(i).writeUTF(line); // LLout.get(i).flush(); } } } } catch (IOException e) { System.out.println("ServerSOut error"); } } } private static class Exit implements Runnable { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("If you want off server write - 'exit'"); while (check) { String line = reader.readLine(); if (line.equals("exit")) { StartServerT.stop(); ServerSOutT.stop(); System.out.println("Server Stop"); check = false; } } } catch (IOException e) { System.out.println("You dont maik off server"); } } } } Class implementing Client :
public class ClientChat { public static Thread t1 = new Thread(new ConectToServer()); public static Thread InServerT = new Thread(new InServer()); public static DataInputStream in; public static String line = null; public static boolean CheckOut = false; public static void main(String[] args) throws IOException { t1.start(); } public static class ConectToServer implements Runnable { public static int serverPort = 6666; public void run() { try { System.out.println("Connect to server..."); ConectToServerURL(); } catch (IOException e) { System.out.println("No connect to server tray again"); } } public void ConectToServerURL() throws IOException { try { String address = "127.0.0.1"; System.out.println("You connect to server"); System.out.println("If you wont exit write 'exit' "); InetAddress ipAddress = InetAddress.getByName(address); Socket socket = new Socket(ipAddress, serverPort); InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream(); in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout); InServerT.start(); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); while (true) { line = keyboard.readLine(); if (line.equals("exit")) { System.out.println("You exit"); t1.stop(); } out.writeUTF(line); out.flush(); CheckOut = true; } } catch (Exception x) { System.out.println("Client not maik connect to server"); } } } public static class InServer implements Runnable { @Override public void run() { try { while (true) { if (CheckOut) { line = in.readUTF(); System.out.println(line); } } } catch (IOException e) { System.out.println("InServer error"); } } } }
LinkedList) where you need to use the fields of individual instances or local variables in general. For some reason, you create threads for what can be done in the main thread (in themainmethod). No thread safety. And finally, the names of the fields and methods must be in small letters (as is customary in java). - Roman