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(); } } 

    1 answer 1

    I on your place would use DataInputStream / DataOutputStream , you have something with sending from the client. If you replace on the client with:

     private String sendMassage(OutputStream out) throws IOException { BufferedReader readCons = new BufferedReader(new InputStreamReader(System.in)); String command = readCons.readLine(); DataOutputStream dos = new DataOutputStream(out); dos.writeUTF(command); return command; } 

    And on the server on:

     private String getMassage(InputStream in) throws IOException { DataInputStream dis = new DataInputStream(in); return dis.readUTF(); } 

    Should take off

    • Thank! It really took off. Strange that without the date of the wrapper did not want ... Magic ... - Pavel
    • @Pavel No magic. Just a BufferedWriter , as the name implies, buffers the output, rather than sending it right away. Yes, and you do not write a line break, then readLine() read it. - Roman
    • Ok understood thanks for the clarification. I'll keep that in mind. - Pavel