输入流未接收到 EOF

发布于 2024-12-07 11:06:59 字数 1604 浏览 0 评论 0原文

我正在尝试通过套接字将图像从我的 Android 设备发送到我的计算机。问题是我的计算机上的输入流读取每个字节,但最后一组字节除外。我尝试过修剪字节数组并发送它,我多次手动将-1写入输出流,但输入流从未读取-1。它只是挂起等待数据。我还尝试过不关闭流或套接字以查看是否是某种计时问题,但这也不起作用。

客户端(Android 手机)

//This has to be an objectoutput stream because I write objects to it first
InputStream is = An image's input stream android
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(object);
objectOutputStream.flush();

byte[] b = new byte[socket.getSendBufferSize()];
int read = 0;
while ((read = is.read(b)) != -1) {
   objectOutputStream.write(b, 0, read);
   objectOutputStream.flush();
   b = new byte[socket.getSendBufferSize()];
}
//Tried manually writing -1 and flushing here
objectOutputStream.close();
is.close();
socket.close();

服务器端(计算机) 这一段代码发生在对象输入流读入发送的对象之后。它仅在文件开始发送时才开始读取

File loc = Location of where the file is stored on the computer
loc.createNewFile();
FileOutputStream os = new FileOutputStream(loc);
Socket gSocket = The socket
ObjectInputStream gInputStream = Object Input stream created from the sockets input stream already used to read in the previous objects

byte[] b = new byte[gSocket.getReceiveBufferSize()];
int read = 0;
while ((read = gInputStream.read(b)) != -1) {
   os.write(b, 0, read);
   os.flush();
   b = new byte[gSocket.getReceiveBufferSize()];
}

os.close();

即使我直接写入-1并刷新流,此代码也不会读入-1。结果是 java.net.SocketException: 当来自 Android 设备的流或套接字关闭时连接重置。图片几乎已完全发送,但图片的最后一个像素是灰色的。我什至尝试直接从套接字使用输出/输入流,而不是使用已经创建的 objectinputstream/objectoutputstream,但它仍然不起作用。

I am attempting to send an image from my android device to my computer via a socket. The problem is the input stream on my computer reads in every single byte but the last set of them. I have tried trimming the byte array down and sending it, I've manually written out -1 to the outputstream multiple times but the inputstream never reads -1. It just hangs waiting for data. I've also tried not closing the stream or sockets to see if it was some sort of timing issue, but that didn't work as well.

Client side (Android Phone)

//This has to be an objectoutput stream because I write objects to it first
InputStream is = An image's input stream android
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(object);
objectOutputStream.flush();

byte[] b = new byte[socket.getSendBufferSize()];
int read = 0;
while ((read = is.read(b)) != -1) {
   objectOutputStream.write(b, 0, read);
   objectOutputStream.flush();
   b = new byte[socket.getSendBufferSize()];
}
//Tried manually writing -1 and flushing here
objectOutputStream.close();
is.close();
socket.close();

Server Side (Computer) This bit of code takes place after the object input stream reads in the objects sent. It only starts to read when the file starts to send

File loc = Location of where the file is stored on the computer
loc.createNewFile();
FileOutputStream os = new FileOutputStream(loc);
Socket gSocket = The socket
ObjectInputStream gInputStream = Object Input stream created from the sockets input stream already used to read in the previous objects

byte[] b = new byte[gSocket.getReceiveBufferSize()];
int read = 0;
while ((read = gInputStream.read(b)) != -1) {
   os.write(b, 0, read);
   os.flush();
   b = new byte[gSocket.getReceiveBufferSize()];
}

os.close();

This code never reads in -1 even if I write -1 directly and flush the stream. The outcome is java.net.SocketException: Connection reset when the stream or socket from the android device is closed. The picture is almost completely sent but the very last pixels of the picture are gray. I also even tried using the out/input stream directly from the socket instead of using the already created objectinputstream/objectoutputstream and it still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-12-14 11:06:59

首先,我认为您误解了 EOF (-1) 的含义。这并不意味着服务器写入了-1,而是意味着服务器关闭了流。

我认为您的主要问题是服务器和客户端都在循环中读取,并且都没有达到关闭流的程度。他们陷入了僵局——双方都在等待对方先关闭。

您的客户端:

您的客户端

您的服务器:

Your server

如果您知道没有更多数据可写,则只需关闭流即可。

Firstly, I think you misunderstood the meaning of EOF (-1). It doesn't mean the server wrote a -1, it means the server closed the stream.

I think your main problem though is that both the server and the client are reading in a loop, and neither get to the point where they close the stream. They are deadlocked - both are waiting for the other one to close first.

Your client:

Your client

Your server:

Your server

If you know that you have no more data to write then just close the stream.

累赘 2024-12-14 11:06:59

由于您已经在使用 ObjectInputStreamObjectOutputStream,因此您可以使用它们各自的 readObjectwriteObject 方法来读取/一次写入整个对象。也许您可以将整个字节数组作为对象发送/接收?

在你的 android 上:

1) byte[] imageBytes = ...; // 包含图像

2) objectOutputStream.writeObject(imageBytes);

在您的计算机上:

1) byte[] imageBytes = (byte[])readObject();

2) 从 imageBytes 获取图像

当然,您必须在线程内使用 readObject,因为它会阻塞。

Since you're already using ObjectInputStream and ObjectOutputStream, you can use their respective readObject and writeObject methods to read/write entire objects at a time. Maybe you could send/receive the entire byte array as an object?

On your android:

1) byte[] imageBytes = ...; // contains the Image

2) objectOutputStream.writeObject(imageBytes);

On your computer:

1) byte[] imageBytes = (byte[])readObject();

2) get image from imageBytes

Of course, you'll have to use readObject from within a thread since it'll block.

桃扇骨 2024-12-14 11:06:59

您正在将 byte[] 数组写入对象,但读取字节。您应该读取对象并将它们转换为 byte[]。 EOS 将导致抛出 EOFException。

You are writing byte[] arrays as objects, bur reading bytes. You should be reading Objects and casting them to byte[]. EOS will cause an EOFException to be thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文