Java simple client server program

前端 未结 3 1616
盖世英雄少女心
盖世英雄少女心 2021-01-26 00:50

I have written a simple java code to send a message from server to client and the client should display it respectively. The codes are.

Client.java

impor         


        
相关标签:
3条回答
  • 2021-01-26 01:05

    You're reading lines but you aren't writing lines. Add a line terminator to the message, or use newline().

    0 讨论(0)
  • 2021-01-26 01:17

    In Server code rather than using BufferedWriter, use PrintWriter.

    while (true) {
            Socket mysocket = server_socket.accept();
    
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);            
            BufferedReader reader = new BufferedReader(new InputStreamReader(mysocket.getInputStream()));
            out.println("Hello World!");
    
            System.out.println("data sent");
        }
    

    This is working on my system. Try this.

    0 讨论(0)
  • 2021-01-26 01:17

    Referring all your answers and searching a bit on google i have finally got the code running by making the following changes in Server Class.

    while (true) {
            Socket mysocket = server_socket.accept();
    
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(mysocket.getOutputStream()));
            BufferedReader reader = new BufferedReader(new InputStreamReader(mysocket.getInputStream()));
            writer.write("Hello omkar \n");
            writer.flush();
            System.out.println("data sent");
        }
    
    0 讨论(0)
提交回复
热议问题