The string read by the client should be transmitted to the server by socket, but not transmitted, I can not understand why.
Here is the code:
Customer:
public class Client { public static void main(String[] args) { new Client().startClient(); } private Socket socket; private void connections() { try { this.socket = new Socket("localhost",5000); } catch (IOException e) { e.printStackTrace(); } } private void startClient() { this.connections(); try (InputStream in = this.socket.getInputStream(); OutputStream out = this.socket.getOutputStream()) { String command = this.sendMassage(out); while (!"q".equals(command)) { System.out.println(command); out.flush(); command = this.sendMassage(out); } } catch (IOException e) { e.printStackTrace(); } } private String sendMassage(OutputStream out) throws IOException { BufferedReader readCons = new BufferedReader( new InputStreamReader(System.in)); BufferedWriter send = new BufferedWriter( new OutputStreamWriter(out,"UTF8")); String command = readCons.readLine(); send.write(command); return command; } } Server:
public class Server { private ServerSocket serverSocket; private Socket socket; public static void main(String[] args) { new Server().startServer(); } private void initServerSocket() { try { this.serverSocket = new ServerSocket(5000); } catch (IOException e) { e.printStackTrace(); } } private void socketAccept() { try { this.socket = this.serverSocket.accept(); } catch (IOException e) { e.printStackTrace(); } } private void startServer() { this.initServerSocket(); System.out.println("Wait..."); this.socketAccept(); System.out.println("ok"); String massage; try (InputStream in = this.socket.getInputStream(); OutputStream out = this.socket.getOutputStream()) { massage = this.getMassage(in); while (!"q".equals(massage)) { System.out.println(massage); massage = this.getMassage(in); } } catch (IOException e) { e.printStackTrace(); } } private String getMassage(InputStream in) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(in,"UTF8")); return br.readLine(); } }