为什么数据输入流在读取二进制文件后不继续接收数据



我正在创建一个服务器来接收来自客户端的文本和二进制数据。它可以处理文本数据以及第一次接收二进制文件,但在此之后,它没有继续读取数据并引发异常。这是我的服务器代码:

public class ConnectedProcessThread implements Runnable{
    private final Socket socket;
    public ConnectedProcessThread(Socket clientSocket){
        socket = clientSocket;
    }
    public void run(){
        DataInputStream dis = null;
        try{
            while(true) {
                dis = new DataInputStream(socket.getInputStream());
                String meta = dis.readUTF();
                Log.i("Data received", meta);
                if(meta.equalsIgnoreCase("Text")){
                    String message = dis.readUTF();
                    Log.i("Data received", message);
                }else if(meta.equalsIgnoreCase("Binary")){
                    InputStream is = socket.getInputStream();
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    byte[] buf = new byte[4096];
                    int len;
                    while((len=is.read(buf))>-1){
                        stream.write(buf,0,len);
                    }
                    stream.flush();
                    //read object input
                    try {
                        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(stream.toByteArray()));
                        byte[] buffer = (byte[])ois.readObject();
                        FileOutputStream fos = new FileOutputStream("/storage/emulated/0/DCIM/IMG-Saved.jpeg");
                        fos.write(buffer);
                    }catch (ClassNotFoundException e){
                        e.printStackTrace();
                    }
                    finally {
                        Log.i("Binary_Transfer","File created");
                    }
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }finally {
            Log.i("Client_Socket","Stream will close");
            if(dis!=null){
                try {
                    dis.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

对于文本和二进制数据,在发送数据之前,客户端会发送文本元数据,通知服务器数据是文本或二进制的。但在接收到文件(图像)后,它在以下行抛出EOFException:String meta=dis.readUTF();我猜发生这种情况是因为在读取和写入二进制文件后,线程继续循环,因此DataInputStream将再次读取,而现在没有任何可接收的内容,因此readUTF()抛出EOFException。在发送二进制文件后,我试图从客户端发送一个元数据,让DataInputStream可以读取一些东西,而不会抛出异常,但它不起作用,客户端确实发送了元数据,但服务器仍然抛出EOFException。有人知道问题出在哪里吗?非常感谢。

这是我从客户端发送的二进制方法:

public void sendBinaryData(byte[] binaryData){
    if(dos!=null && socket!=null){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(binaryData);
            Log.d("Binary_Transfer", "C: Sent.");
            oos.close();
            dos.writeUTF("Binary_End");
            dos.flush();
        }catch (Exception e){
            Log.e("File_Exception",e.toString());
        }
    }
}

因为当您开始读取二进制数据时,您会进入一个仅在流结束时终止的循环,即当对等方断开连接时:

while((len=is.read(buf))>-1){
    stream.write(buf,0,len);
}

在这一点上,你已经到了溪流的尽头。没有更多的数据,也没有更多的资料,也永远不会有更多的资料。

你需要完全删除这个部分:

InputStream is = socket.getInputStream();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int len;
while((len=is.read(buf))>-1){
    stream.write(buf,0,len);
}
stream.flush();

无论如何,将事物读入ByteArrayOutputStreams中几乎没有任何意义,这也不例外。只需完全删除它,然后更改下一部分:

ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(stream.toByteArray()));

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

继续你已经做的。然而,你有另一个问题:

oos.close();

这将关闭插座。所以下一部分:

dos.writeUTF("Binary_End");
dos.flush();

不可能工作。只需冲洗ObjectOutputStream而不是关闭它。

但我强烈建议您放弃DataInput/OutputStreams,并在套接字的两端使用单个ObjectInput/OutputStream

最新更新