Java:简单客户端服务器消息交换不起作用

时间:2018-07-23 21:58:58

标签: java

我是Java的新手,昨天才开始学习。我写了一个非常简单的客户端服务器Java代码。客户端向服务器发送消息。服务器应显示该消息。并且服务器应在收到消息后向客户端发送消息。客户端应显示服务器发送的消息。

服务器代码

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.net.Socket;
import java.net.ServerSocket;

public class CustomServer{
    public static void main(String[] args){
       final int SERVER_PORT_NUMBER = 8081;

       try{
         ServerSocket serverObj = new ServerSocket(SERVER_PORT_NUMBER);
         Socket clientSocketObj = serverObj.accept();
         BufferedReader clientInputStream = new BufferedReader(new InputStreamReader(clientSocketObj.getInputStream()));
         BufferedWriter clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocketObj.getOutputStream()));

        if(clientSocketObj != null){
            System.out.println("Client Connected to Server!");

            // Recieve Message from Client
            System.out.println("MESSAGE FROM CLIENT");
            System.out.println(clientInputStream.readLine());

            // Send Message to Client
            clientOutputStream.write("SERVER: Hello Client!");

            // Close Streams
            clientOutputStream.close();
            clientInputStream.close();
        }
        serverObj.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}
}

客户

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

public class CustomClient{
    public static void main(String[] args){
        final String HOST_NAME = "127.0.0.1";
        final int SERVER_PORT_NUMBER = 8081;

        try{
           Socket clientSocket = new Socket(HOST_NAME, SERVER_PORT_NUMBER);
           BufferedWriter clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
           BufferedReader clientInputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
           System.out.println("Connecting....");

        if(clientSocket != null){
            System.out.println("Connected to Server!");

            // Send message to Server
            clientOutputStream.write("CLIENT: HELLO SERVER");

            // Recieve message from Server
            System.out.println("MESSAGE FROM SERVER");
            System.out.println(clientInputStream.readLine());

            // Close Streams
            clientInputStream.close();
            clientOutputStream.close();
        }
        clientSocket.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}
}

服务器或客户端均未收到该消息。卡在某个循环中。有人知道为什么吗?

1 个答案:

答案 0 :(得分:1)

首先阅读BufferedReader's JavaDocs,该状态为

  

读取一行文本。一行被认为由换行符('\ n'),回车符('\ r')或回车符后立即换行符中的任何一个终止

BufferedWriter#write未发送此消息,因此读者仍在等待。

一个简单的解决方案可能是在write之后使用BufferedWriter#newLine

写完缓冲区后,不要忘记flush

您可能还想看看try-with-resources,它将提供更好的资源管理解决方案

CustomClient

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class CustomClient {

    public static void main(String[] args) {
        final String HOST_NAME = "127.0.0.1";
        final int SERVER_PORT_NUMBER = 8081;

        try (Socket clientSocket = new Socket(HOST_NAME, SERVER_PORT_NUMBER)) {
            try (BufferedWriter clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
                    BufferedReader clientInputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {
                System.out.println("Connecting....");
                System.out.println("Connected to Server!");

                // Send message to Server
                clientOutputStream.write("CLIENT: HELLO SERVER");
                clientOutputStream.newLine();
                clientOutputStream.flush();

                // Recieve message from Server
                System.out.println("MESSAGE FROM SERVER");
                System.out.println(clientInputStream.readLine());
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

CustomServer

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class CustomServer {

    public static void main(String[] args) {
        final int SERVER_PORT_NUMBER = 8081;

        try (ServerSocket serverObj = new ServerSocket(SERVER_PORT_NUMBER)) {
            try (Socket clientSocketObj = serverObj.accept()) {
                try (BufferedReader clientInputStream = new BufferedReader(new InputStreamReader(clientSocketObj.getInputStream()));
                        BufferedWriter clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocketObj.getOutputStream()))) {

                    System.out.println("Client Connected to Server!");

                    // Recieve Message from Client
                    System.out.println("MESSAGE FROM CLIENT");
                    System.out.println(clientInputStream.readLine());

                    // Send Message to Client
                    clientOutputStream.write("SERVER: Hello Client!");
                    clientOutputStream.newLine();
                    clientOutputStream.flush();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}