Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用BufferedReader读取DataInputStream中的内容_Java_Sockets - Fatal编程技术网

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

Java 如何使用BufferedReader读取DataInputStream中的内容,java,sockets,Java,Sockets,我编写了一个关于在客户端和服务器之间发送/接收消息的简单套接字教程。我使用DataOutputStream在流中写入字符串,但如果我使用BufferedReader 如果我使用PrintWriter来编写(客户端),它就可以工作 这里怎么了?太多了 1。客户: client=newsocket(“localhost”,1982年); DataOutputStream opStr=新的DataOutputStream(client.getOutputStream()); //pw=新的PrintW

我编写了一个关于在客户端和服务器之间发送/接收消息的简单套接字教程。我使用DataOutputStream在流中写入字符串,但如果我使用BufferedReader

如果我使用PrintWriter来编写(客户端),它就可以工作

这里怎么了?太多了

1。客户:

client=newsocket(“localhost”,1982年);
DataOutputStream opStr=新的DataOutputStream(client.getOutputStream());
//pw=新的PrintWriter(client.getOutputStream(),true);
//println(“你好,外面有人吗?”);//可以由BufferedReader读取
“喂,有人吗?”;
optstr.flush();//BufferedReader无法读取它
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);
                }
            }

你不能。如果在一端使用
writeUTF()
,则必须在另一端使用
readUTF()


您只需决定要使用哪种API,而不是尝试混合和匹配它们。

您希望以文本(如BufferedReader)或二进制(如DataInputStream)的形式读取文件。所以你不能两者都用

Server.java
我认为你写数据的方式只允许你以这种方式读取数据。。。像缓冲读写器。。。数据读写器。。。查看您在上面的服务器中评论的行。。。指令readUTF();这就是我的意思…我发现:BufferedReader不能直接包装InputStream。它包装了另一个读者。我只需要更改为新的BufferedReader(新的InputStreamReader(clientSocket.getInputStream(),“UTF-8”)。Tks somuch@ducanhng不。这并不能为您提供一个使用
BufferedReader读取
writeUTF()
输出的解决方案。
没有。
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);
                    }
                }
            }
        }
    }

}