从套接字读取二进制数据 [英] Read binary data from a socket

查看:80
本文介绍了从套接字读取二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到一个服务器,然后发送一个HTTP请求(在这种情况下为GET)。这个想法是请求一个文件,然后从服务器接收它。

它应该同时处理文本文件和二进制文件(例如imgs)。我有没有问题的文本文件,它的工作完美,但我有一些麻烦的二进制文件。



首先,我声明一个BufferedReader(用于读取头文件和文本文件)和DataInput流:

  BufferedReader in_text = new BufferedReader(
new InputStreamReader(socket.getInputStream())) ;

DataInputStream in_binary = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));

然后,我用in_text读取头并发现它是否是文本文件或二进制文件。如果它是一个文本文件,我在StringBuilder中正确读取它。如果是二进制文件,我声明一个字节[filesize]并存储in_binary的以下内容。

  byte [] bindata =新字节[文件大小]; 
in_binary.readFully(bindata);

它不起作用。我得到一个EOFException。



我认为in_binary仍然在流的第一个位置,所以它还没有读取头。所以我捕获了头部的长度并跳过in_binary中的那些字节。

  byte [] bindata = new byte [filesize]; 
in_binary.reset();
in_binary.skip(headersize);
in_binary.readFully(bindata);

而且仍然相同。

什么可能会发生?



谢谢!

PD:我知道我可以使用URLConnection和所有这些。这不是问题。

解决方案

BufferedReader buffers data ) - 它几乎肯定会从套接字读取更多的数据,而不仅仅是头部。因此,当您尝试读取某些已经从套接字读取的实际数据时。如果您尝试读取几个字节,您可能会发现它们不是实际响应数据的第一个字节。



如果您知道如何使用 URLConnection ,我不知道你有什么理由不使用它。


I'm trying to connect to a server, and then send it a HTTP request (GET in this case). The idea is request a file and then receive it from the server.

It should work with both text files and binary files (imgs for example). I have no problem with text files, it works perfect, but I'm having some troubles with binary files.

First, I declare a BufferedReader (for reading header and textfile) and a DataInput Stream:

BufferedReader in_text = new BufferedReader(
    new InputStreamReader(socket.getInputStream()));

DataInputStream in_binary = new DataInputStream(
    new BufferedInputStream(socket.getInputStream()));

Then, I read the header with in_text and discover if it's a textfile or binary file. In case it's a textfile, I read it correctly in a StringBuilder. In case it's a binary file, I declare a byte[filesize] and store the following content of in_binary.

byte[] bindata = new byte[filesize];
in_binary.readFully(bindata);

And it doesn't work. I get a EOFException.

I thought that maybe in_binary is still in the first position of the stream, so it hasn't read the header yet. So I captured the length of the header and skip that bytes in in_binary.

byte[] bindata = new byte[filesize];
in_binary.reset();
in_binary.skip(headersize);
in_binary.readFully(bindata);

And still the same.

What could be happening?

Thanks!

PD: I know I could use URLConnection and all of that. That's not the problem.

解决方案

BufferedReader buffers data (hence the name) - it will almost certainly have read more data from the socket than just the header. Therefore, when you try to read the actual data some has already been read from the socket. If you try reading just a few bytes you'll probably see that they aren't the first bytes of the actual response data.

If you know how to use URLConnection, I have to wonder what reason you have for not using it.

这篇关于从套接字读取二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆