Java 套接字 - 读取和写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38668203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:38:58  来源:igfitidea点击:

Java socket - Read & Write

javasockets

提问by Shlomi

The problem: Client doesn't receive any message.

问题:客户端没有收到任何消息。

Here is the full code for both client and server:

这是客户端和服务器的完整代码:

CLIENT

客户

public class Client extends Socket{


public Client(String hostName, int port) throws UnknownHostException, IOException {
    super(hostName,port);

    BufferedReader in = new BufferedReader(new InputStreamReader(getInputStream()));

    while(true) {
        String line = in.readLine();
        System.out.println("Text received: " + line);
    }

}

SERVER

服务器

public final class Server extends ServerSocket{

public Server(int port) throws IOException {
    super(port);

    System.out.println("Server waiting for client 1");
    Socket client1 = accept();
    PrintWriter writer = new PrintWriter(client1.getOutputStream(), true);
    writer.write("Hello user 1");

    System.out.println("Server waiting for client 2");
    Socket client2 = accept();
    PrintWriter writer2 = new PrintWriter(client2.getOutputStream(), true);
    writer2.write("Hello user 2");

    System.out.println("Clients connected");

}
  • I start the server to listen to port 4444
  • I start the clients with hostname of "localhost" and port 4444
  • 我启动服务器监听4444端口
  • 我以“localhost”的主机名和端口 4444 启动客户端

回答by Nerdizzle

You have to include a newline character at the end of the message, and also flush the PrintWriter if the connection is not immediately altered, forcing an automatic flush:

你必须在消息的末尾包含一个换行符,如果连接没有立即改变,还要刷新 PrintWriter,强制自动刷新:

writer.write("Hello user 1\n");
writer.flush();

EDIT:

编辑:

It is possible to enable automatic flushing on a PrintWriter using the constructor new PrintWriter(someOutputStream, true)

可以使用构造函数在 PrintWriter 上启用自动刷新 new PrintWriter(someOutputStream, true)

However, as explained in the documentation:

但是,如文档中所述:

if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output

如果启用了自动刷新,则只有在调用 println、printf 或 format 方法之一时才会执行,而不是在刚好输出换行符时执行

This means that even with automatic flushing, the PrintWriter would still have to be manually flushed after writeis called, and a newline character (\n) would still have to be included at the end of the string.

这意味着即使使用自动刷新,在write调用PrintWriter 后仍然必须手动刷新,并且\n仍然必须在字符串的末尾包含换行符 ( )。

EDIT 2:

编辑2:

Here is a small example of a fully functional client/server application:

这是一个功能齐全的客户端/服务器应用程序的小例子:

Client:

客户:

public static void main(String[] args){
    try{
        Socket socket = new Socket(HOST_ADDRESS, PORT);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(bufferedReader.readLine());
        bufferedReader.close();
        socket.close();
    }catch(IOException e){}
}

Server:

服务器:

public static void main(String[] args){
    try{
        ServerSocket serverSocket = new ServerSocket(PORT);
        Socket socket = serverSocket.accept();
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
        printWriter.write("Hello user!\n");
        printWriter.flush();
        printWriter.close();
        socket.close();
        serverSocket.close();
    }catch(IOException e){}
}