简体   繁体   English

如何使用BufferedReader读取DataInputStream中的内容

[英]How to use BufferedReader to read content in DataInputStream

I wrote a simple socket tutorial about sending/receive messages between client and server. 我写了一个简单的套接字教程,关于在客户端和服务器之间发送/接收消息。 I used DataOutputStream to write the string in stream but server couldn't read it if I used BufferedReader 我使用DataOutputStream在流中写入字符串,但是如果我使用BufferedReader ,则服务器无法读取它

If I use PrintWriter to write(client side), it works. 如果我使用PrintWriter进行写(客户端),则可以使用。

What's wrong here? 怎么了 Tks so much. 非常感谢。

1. Client: 1.客户:

    client = new Socket("localhost", 1982);
    DataOutputStream opStr = new DataOutputStream(client.getOutputStream());

    //pw = new PrintWriter(client.getOutputStream(), true);
    //pw.println("Hello, is there anybody out there?");// Can be read by BufferedReader
    opStr.writeUTF("Hello, anyone there?");
    opStr.flush();// BufferedReader can't read it

2. Server: 2.服务器:

        openServer();// port 1982
        while(true) {
            Socket clientSocket = null;

                // listen to connection.
                clientSocket = echoServer.accept();

                DataInputStream inStr = new DataInputStream(
                        clientSocket.getInputStream());

                //System.out.println("M1: Got msg " + inStr.readUTF());// It showed the content

                BufferedReader bfReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                System.out.println("got Messages: ");
                String strLine = "";
                // Don't show anything
                while ((strLine = bfReader.readLine()) != null) {
                    System.out.println(strLine);
                }
            }

You can't. 你不能 If you use writeUTF() at one end, you have to use readUTF() at the other end. 如果在一端使用writeUTF() ,则必须在另一端使用readUTF()

You just have to decide which API you're going to use, instead of trying to mix and match them. 您只需确定要使用的API,而不必尝试将它们混合使用。

You want to read the files as either text eg BufferedReader OR binary eg DataInputStream. 您想以文本(例如BufferedReader)或二进制(例如DataInputStream)的形式读取文件。 So you can't use both. 因此,您不能同时使用两者。

Server.java Server.java

public class Server 
{
    public static void main(String[] args) 
    {
        DataInputStream inStr = null;
        String str;
        openServer();// port 1982
        while(true) 
        {
            Socket clientSocket = null;
            // listen to connection.
            clientSocket = echoServer.accept();
            try
            {
                inStr = new DataInputStream(clientSocket.getInputStream());
                str = inStr.readUTF();
                System.out.print((String) str);
                System.out.flush(); 
            } 
            catch (IOException io) 
            {
                System.err.println("I/O error occurred: " + io);
            } 
            catch (Throwable anything) 
            {
                System.err.println("Exception caught !: " + anything);
            }
            finally 
            {
                if (inStr != null) 
                 {
                    try 
                    {
                        inStr.close();
                    } 
                    catch (IOException io) 
                    {
                      System.err.println("I/O error occurred: " + io);
                    }
                }
            }
        }
    }

}

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

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