Java服务器客户端通信似乎卡住了



你好stackoverflow社区,

我遇到了有关Java插座通信的问题。

这是我的服务器和客户端类的示例代码:

服务器:

public class OTPServer {

static ServerSocket serverSocket;
final static int PORT = 4242;
static Socket clientConnection;
public static void main(String[] args) {
    try {
        serverSocket = new ServerSocket(PORT);
        System.out.println("Socket initialized");
        String serverMessage = "Hello, I am the Host";
        ServerTool serverTool = new ServerTool();
        while (true) {
            clientConnection = serverSocket.accept();
            if(clientConnection.isConnected()) {
                System.out.println("Client connected");
            }
            BufferedReader clientInputReader = new BufferedReader(new InputStreamReader(clientConnection.getInputStream()));
            DataOutputStream serverOutput = new DataOutputStream(clientConnection.getOutputStream());
            System.out.println("Sending message to client: " + serverMessage);
            serverOutput.writeBytes(serverTool.encodeMessage(serverMessage));
            serverOutput.flush();
            String clientMessage = clientInputReader.readLine();
            System.out.println("Encoded answer from client: " + clientMessage);
            String decodedMessage = serverTool.decodeMessage(clientMessage);
            System.out.println("Decoded answer from client: " + decodedMessage);
            serverOutput.close();
            clientInputReader.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Hello, I am the OTP Server!");
}

这是客户:

public class OTPClient {
static Socket clientSocket;
final static int PORT = 4242;
final static String HOST = "localhost";

public static void main(String[] args) {
    System.out.println("I am the OTP Client!");
    String serverMessage;
    String clientResponse = "I am the Client";
    OTPTool otpTool = new OTPTool();
    try {
        clientSocket = new Socket(HOST, PORT);
        BufferedReader serverInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
        System.out.println("Connection to Host established");
        serverMessage = serverInput.readLine();
        System.out.println("Encoded Message from Server: " + serverMessage);
        String decodedMessage = otpTool.decodeMessage(serverMessage);
        System.out.println("Decoded message from Server: " + decodedMessage);
        System.out.println("Answering with own message: " + clientResponse);
        outputStream.writeBytes(clientResponse);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

现在我的问题在哪里:

连接建立,服务器似乎将其消息发送给客户端并等待答案。客户端未打印他从服务器收到的消息。一旦我取消服务器,客户端就会打印从服务器和信息中获取的消息,答案是带有退出代码0的答案出口,因此看来此部分很好,它只是以某种方式被卡住了。我已经尝试像您在给出的示例代码中所看到的那样刷新outputStream。

是否有明显的我缺少的东西?我知道,这确实是基本的东西,但这是我第一次使用插座进行通信。

预先感谢您!

最好的问候,罗尼

btw:我知道服务器仅连接到一个请求连接的客户端。那绝对足以让我使用。

它被卡住了,因为 serverInput.readLine();块会块,直到遇到文件中断或文件结束为止。在服务器端,您没有发送换行符,因此客户端块。

相关内容

  • 没有找到相关文章

最新更新