简体   繁体   English

Socket 编程中的 BufferedReader

[英]BufferedReader in Socket Programming

When i am trying to send an input from a client, if i don't concat "\r\n" at the end of the string, my inputstream waits forever.当我尝试从客户端发送输入时,如果我不在字符串末尾连接“\r\n”,我的输入流将永远等待。 I have seen various similar posts but couldn't find a proper solution.我看过各种类似的帖子,但找不到合适的解决方案。 My code is as follows:我的代码如下:

public void run() {
    
    PrintWriter out = null;
    BufferedReader in = null;
    try {
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        String line;

        if (in.ready()) {
            if ((line = in.readLine()) != null) {
                System.out.println("Received from client: " + line);
                out.write("Echoing: " + line);
                out.flush();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
        try {
            in.close();
            clientSocket.close();
            System.out.println("Closing connection from " + socketAddress + ", #" + connectionId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

If you only want to read some of the data that was sent, use the read(char[]) method instead of the readLine method.如果您只想读取已发送的部分数据,请使用read(char[])方法而不是readLine方法。 This method returns the number of characters it read as an int .此方法以int形式返回读取的字符数。 Example:例子:

     char[] buffer = new char[2000];
     int read;
     if ((read = in.read(buffer)) != -1) {
            String line = new String(buffer, 0, read);
            System.out.println("Received from client: " + line);
            out.write("Echoing: " + line);
            out.flush();
      }

What you'll see happen next is that this code sometimes fails to read the entire message you send from PHP, or that two or more messages are read as one.接下来您会看到此代码有时无法读取您从 PHP 发送的整个消息,或者将两条或多条消息作为一条读取。

If you want to fix that:如果你想解决这个问题:

This will never work, and it's not because of PHP or Java.这永远不会奏效,也不是因为 PHP 或 Java。 You're using TCP, a stream oriented protocol.您正在使用 TCP,一个面向 stream 的协议。 There is no guarantee that the message you write to the socket from the PHP program will arrive in one piece to the receiver.无法保证您从 PHP 程序写入到套接字的消息将一体地到达接收器。 The message may be broken up and you need multiple socket function calls to read it.该消息可能被分解,您需要多个套接字 function 调用来读取它。 Or it may go the other direction, and a single call to read returns more than one message.或者它可能 go 另一个方向,并且单个调用read返回多个消息。

The solution is to add some kind of framing for the messages so that the receiver knows when it has a complete message.解决方案是为消息添加某种框架,以便接收者知道何时收到完整的消息。 Always ending a message with a line break serves as framing, if messages themselves are single lines.如果消息本身是单行,则始终以换行符结束消息作为框架。 Another solution is to ditch TCP and use a message oriented protocol (like UDP) instead, but that will come with its own complications.另一种解决方案是放弃 TCP 并改用面向消息的协议(如 UDP),但这会带来其自身的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM