如何解决 Server Socket没有用Java发送数据?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (94)

我刚学会了如何socket在Java中创建s和所有好东西,所以我的第一次尝试从客户端给我一条消息,然后客户端崩溃。应该发生的是从客户端获取消息,如果该消息等于此,则返回数据。然而,if对于该消息是否是正确的并没有发射功能,即使消息正确的。

这是带有删除if功能的服务器代码:

try {
    System.out.println("Waiting for a connection...");
    // Start a server
    ServerSocket server = new ServerSocket(3211);
    // Listen for anyone at that port
    Socket socket = server.accept();
    System.out.println("The client has connected!");
    // Get the data being sent in
    DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    DataOutputStream ouputStream = new DataOutputStream(socket.getOutputStream());
    // Turn that into UTF-8
    String data = inputStream.readUTF();
    System.out.println("Received " + data);
    ouputStream.writeUTF("Great!");
    System.out.println("Awesome!");
    socket.close();
    inputStream.close();
    ouputStream.close;
    server.close();
    System.out.println("Socket closed\n-----------------------------");
}
catch(IOException e) {
    System.out.println(e);
}

和客户端(按下按钮时触发):

try {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnecting to the server...");
    Socket socket = new Socket("192.168.1.206", 3211);
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnected to the server!");
    DataInputStream input = new DataInputStream(System.in);
    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    output.writeUTF("sweet");
    String data = input.readUTF();
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nSERVER: " + data);
    input.close();
    output.close();
    socket.close();

}
catch (IOException er) {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\n" + er);
}

只要按下按钮尝试启动连接(服务器已经运行),客户端就会立即冻结。它甚至没有发送任何“连接到服务器”的东西,如何解决这个问题?

提问于
用户回答回答于

客户正在阅读System.in,应该是从套接字输入流中读取的,注意只需要关闭套接字的其他流,如果需要,会刷新它并关闭输入流和套接字

用户回答回答于

socket无法发送数据,因为没有.flush()在outputstream引用上调用方法,使用这个不必在流上明确地编写flush()和close()方

服务器代码

System.out.println("Waiting for a connection...");
// Start a server
try (ServerSocket server = new ServerSocket(3211)) {
    // Listen for anyone at that port
    try (Socket socket = server.accept()) {
        System.out.println("The client has connected!");
        // Get the data being sent in
        try (DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()))) {
            try (DataOutputStream ouputStream = new DataOutputStream(socket.getOutputStream())) {
                // Turn that into UTF-8
                String data = inputStream.readUTF();
                System.out.println("Received " + data);
                ouputStream.writeUTF("Great!");
                System.out.println("Awesome!");
           }
        }
    }
    System.out.println("Socket closed\n-----------------------------");
} catch (IOException e) {
    System.out.println(e);
}

客户代码

try {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnecting to  the server...");
    try (Socket socket = new Socket("127.0.0.1", 3211)) {
        allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnected to the server!");
        try (DataInputStream input = new DataInputStream(socket.getInputStream())) {
            try (DataOutputStream output = new DataOutputStream(socket.getOutputStream())) {
                output.writeUTF("sweet");
            }
            String data = input.readUTF();
            System.out.println(String.format("data received from server '%s':\n", data));
            allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nSERVER: " +  data);
        }
    }
} catch (IOException er) {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\n" + er);
}

服务器输出

Waiting for a connection...
The client has connected!
Received sweet
Awesome!
Socket closed
-----------------------------

客户输出

data received from server 'Great!':

扫码关注云+社区

领取腾讯云代金券