在同一个流上发送二进制数据和字符串 [英] Sending both binary data and strings over the same stream

查看:97
本文介绍了在同一个流上发送二进制数据和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我现在已经陷入了一个小问题,我似乎不知道如何解决它。我想要做的是以下内容:

通过套接字发送:

1.要发送的文件数(不是整数,只是作为字符串)

然后为每个文件发送:

2.文件名长度(再次作为字符串)

3.文件名

4.作为二进制数据存档。


我抓住了我的核心Java书,并认为使用缓冲数据输入/输出流很容易实现,因为我可以投它是一个缓冲的数据流,用于文件中的二进制数据,以及用于发送/接收字符串的数据流(甚至是一种有效的思维方式?)


整个'当我尝试实现它时,'plan''刚刚开始崩溃,它说不推荐使用DataInputStream方法。我不能使用读卡器,因为我必须能够接收二进制数据...


现在,我认为这将是一个严重的错误(请纠正我如果我错了)将相同的输入/输出流传递给阅读器,并手工构造一个bufferedInput / OutputStream,其输入/输出流与阅读器使用的输入/输出流相同。并且在第二个想法,同样的问题可能发生在将数据流包装为DataInputStream(新的BufferedInputStream(socket.getInputstream()))所以我只是抓住那个计划......


所以......我有点卡在这里......我怎样才能在同一个套接字上发送二进制数据和字符串?虽然没有那么复杂,但是使用可以与java实现通信的相同协议构建正确工作的C ++对应物真的很困难。


提前谢谢。


汤姆。

解决方案

嗨汤姆!


我检查了 DataInputStream API 似乎readLine()方法是折旧的,但不是整个类。


我认为,以你建议的方式重用流不应该是一个问题。


哦,对于发送字符串,你也可以使用 ObjectOutputStream / ObjectInputStream (虽然在尝试创建C ++版本时可能会出现问题,但我真的不知道)。


问候,

Nepomuk




感谢您的回复:)。


我不确定的是什么重用流是关闭流和缓冲数据。

如果数据进入原始流,它是否被复制到,例如BufferedInputStream的缓冲区和BufferedReader的缓冲区,还是仅存在于其中一个缓冲区中?

后一种情况会很成问题,因为我永远不知道我的数据在哪里。如果它是前者那么我应该确切知道每个流中要跳过多少字节,就像我从另一个流中读取的那样,但这不应该很难。


我正在考虑的另一件事是,如果我采用一个纯字节流并且只是为了所有我认识的字符(例如,直到2字节的强制转换结果导致换行符)而将其转换为char'然后解释它应该是的下一部分。

但是,我可以用c ++做到这一点但我不知道在java中这样做。

你对此有什么想法吗?


提前致谢,

Tom


< blockquote>如果您只是发送二进制文件,您应该能够在发送文件后获得其名称和长度,对吗?


Hi,

I''ve bumbed into a slight problem now, and I just don''t seem to know how to fix it. What I want to do is the following:
Send over a socket:
1. Number of files to be send (not as an integer, just as a string)
then for each file to be send:
2. Length of Filename (again as a string)
3. Filename
4. File as binary data.

I grabbed my Core Java book and figured it''d be easily doable using a bufferd data input/output stream, As i can cast it to a buffered data stream for the binary data in the file, and to the data stream for sending/receiving strings (is that even a valid way of thinking ?)

The whole ''plan'' just started crumbling down when I tried to implement it and it said that the DataInputStream method is deprecated. And I can''t use a Reader as I must be able to receive binary data...

Now, I think it''d be a grave mistake (please do correct me if I''m wrong) to hand the same input/output stream over to a reader, and hand construct a bufferedInput/OutputStream with the same input/output stream as is being used by the reader. And on second thought, the same problem probably occurs with Wrapping the stream in as a DataInputStream(new BufferedInputStream ( socket.getInputstream())) so i''ll just scratch that plan too...

So... I''m kinda stuck here... How can I send both binary data and strings over the same socket ? While not making it that complicated that it''s really difficult to construct a correctly working C++ counterpart using the same protocol that can communicate with the java implementation.

Thanks in advance.

Tom.

解决方案

Hi Tom!

I checked the DataInputStream API and it seems that the readLine() method is depreciated, but not the whole class.

Also I think, it shouldn''t be a problem to reuse a stream in the way you suggested.

Oh, and for sending Strings, you could also use an ObjectOutputStream / ObjectInputStream (although that might be problematic when trying to create a C++ counterpart, I really don''t know that).

Greetings,
Nepomuk


Hi,

Thanks for the reply :).

What I''m not sure about with the reusing of a stream is both the closing of the stream, and the buffered data.
If data enters on the original stream, is it copied to, for example both the buffer for the BufferedInputStream and to the buffer of a BufferedReader, or is it only present in one of the buffers ?
The latter case would be quite problematic as I would never know where my data is at. If it''s the former then I should know exactly how many bytes to skip in each stream as I read from the other Stream, but that shouldn''t be to hard.

Another thing I was thinking about is, how about if I take a pure bytestream and just cast it char''s for example for all the things I know are chars (for example until the result of a cast of 2 bytes results in a newline) and then interpret the next part as it is supposed to be.
But tbh, I could do that in c++ But i have no idea whatsoever on doing this in java.

Do you have any idea''s about that ?

Thanks in advance,
Tom


If you just send the binary file you should be able to get both its name and length after sending the file, right?


这篇关于在同一个流上发送二进制数据和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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