简体   繁体   English

通过DataInputStream连续读取套接字消息的正确方法是什么?

[英]What's the proper way to continuously read socket messages through DataInputStream?

I'm trying to build a Java Bittorent client. 我正在尝试构建一个Java Bittorent客户端。 From what I understand after peers handshake with one another they may start sending messages to each other, often sending messages sporadically. 据我所知,在同伴互相握手之后,他们可能会开始互相发送消息,经常偶尔发送消息。

Using a DataInputStream connection I can read messages, but if I call a read and nothing is on the stream the peers holds. 使用DataInputStream连接我可以读取消息,但如果我调用一个读取,并且流上没有任何内容,则对等端保持。 Is there a way I can tell if something is being sent over the stream? 有没有办法告诉我是否通过流发送了什么? Or should I create a new thread that reads the stream for messages continuously from each peer until the client shuts them down shut down? 或者我应该创建一个新线程,从每个对等端连续读取消息流,直到客户端关闭它们为止?

I think you need to do some major experimenting so that you can start to learn the basics of socket I/O. 我认为你需要做一些重大的实验,以便你可以开始学习socket I / O的基础知识。 Trying to answer your question "as is" is difficult, because you don't yet understand enough to ask the question in a manner that it can be answered. 试图“按现状”回答你的问题很困难,因为你还没有足够的理解以一种可以回答问题的方式提问。

If you want to be able to tell if there is data to read, then you should not use the blocking I/O approach. 如果您希望能够判断是否有要读取的数据,则不应使用阻塞I / O方法。 Instead, you will need to use the APIs known as "NIO", which allow you to "select" a socket that has data to read (ie a socket that is associated with a buffer that already has data in it). 相反,您将需要使用称为“NIO”的API,它允许您“选择”具有要读取的数据的套接字(即与已在其中包含数据的缓冲区关联的套接字)。

This will make much more sense after you write a lot of code and mess it up a few times. 在编写大量代码并将其搞砸几次后,这将更有意义。 The underlying I/O primitives are actually quite primitive (pun intended). 底层的I / O原语实际上非常原始(双关语)。 In this industry, we just made up lots of complicated terms and function names and API descriptions so that people would think that network communication is magic. 在这个行业中,我们只是编写了许多复杂的术语和函数名称以及API描述,以便人们认为网络通信是神奇的。 It's not. 不是。 It's generally no more complicated than memcpy(). 它通常不比memcpy()复杂。

There is a function in C called select() . C中有一个叫做select()的函数。 In the scenario you've described, you need an equivalent of select in Java. 在您描述的场景中,您需要等效的Java select And that is, as cpurdy mentioned, Non-blocking Socket I/O or NIO. 就像cpurdy提到的那样,非阻塞套接字I / O或NIO。 Cursory googling returned following links: 粗略的谷歌搜索返回以下链接:

You might want to take a look at the Netty project: http://netty.io/ 您可能想看一下Netty项目: http//netty.io/

It is very easy with Netty to get started on network programming. Netty很容易开始进行网络编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用datainputstream从套接字读取 - How to read from socket using datainputstream 从控制台连续读取消息并执行其他操作的正确方法 - correct way to continuously read messages from the console and do other things Android应用中带有套接字DataInputStream BufferedInputStream的错误消息序列 - Wrong messages sequence with socket DataInputStream BufferedInputStream in Android app 通过 SocketChannel 读取(写入)的正确方法 - Proper way to read (write) through a SocketChannel 发送套接字发送但接收套接字从未读取的消息会怎样? - What happens to messages sent by sending socket but never read by receiving socket? 使用dataoutputstream将字节写入套接字时,如何读取datainputstream? - How to read a datainputstream when using a dataoutputstream to write bytes to socket? 使用DataInputStream / socket从GPS设备读取数据 - Read data from GPS Device using DataInputStream/socket 在Java Socket中,DataInputStream不能完全读取数据 - In Java Socket,DataInputStream doesn't read the Data completely 通过套接字以独立于语言的方式交换消息/对象 - Exchanging messages/objects through socket in language independent way HTML输入转义 - 什么是正确的方法? - HTML input escaping - what's the proper way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM