非阻塞套接字
在 Java 中实现非阻塞套接字的最佳方法是什么?
或者有这样的事情吗?我有一个通过套接字与服务器通信的程序,但如果数据/连接出现问题,我不希望套接字调用阻塞/导致延迟。
What's the best way to implement a non-blocking socket in Java?
Or is there such a thing? I have a program that communicates with a server through socket but I don't want the socket call to block/cause delay if there is a problem with the data/connection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 非阻塞套接字是在 Java 2 标准版 1.4 中引入的。它允许使用套接字的应用程序之间进行网络通信,而不会阻塞进程。但是 Teo,什么是非阻塞套接字?它在哪些上下文中有用?以及它是如何工作的?好吧,年轻的学徒,让我们回答这些问题。
什么是非阻塞套接字?
非阻塞套接字允许在通道上进行 I/O 操作,而不会阻塞使用它的进程。这意味着,我们可以使用单个线程来处理多个并发连接并获得“异步高性能”读/写操作(有些人可能不同意)。
好的,它在哪些情况下有用?
假设您想实现一个接受不同客户端连接的服务器。还假设您希望服务器能够同时处理多个请求。使用传统方式开发这样的服务器有两种选择
:实现一个多线程服务器,手动处理每个连接的线程。
b.使用外部第三方模块。
两种解决方案都有效,但采用第一种解决方案时,您必须开发整个线程管理解决方案,并带来相关的并发和冲突问题。第二种解决方案使应用程序依赖于非 JDK 外部模块,并且您可能必须使该库适应您的需要。通过非阻塞套接字,您可以实现非阻塞服务器,而无需直接管理线程或借助外部模块。
它是如何运作的?
在详细介绍之前,您需要了解几个术语:
Java NIO 有一个名为
的类选择器
允许单个线程检查多个通道上的 I/O 事件。这怎么可能?那么,选择器
可以检查通道的“准备情况”,以了解诸如客户端尝试连接或读/写操作之类的事件。也就是说,Selector
的每个实例都可以监控更多套接字通道,从而监控更多连接。现在,当通道上发生某些事情(发生事件)时,选择器
会通知应用程序处理请求。选择器
通过创建事件键(或选择键)来完成此操作,这些事件键是SelectionKey
类的实例。每个键
都包含有关谁发出请求以及请求的类型的信息,如图 1 所示。图 1:结构图
基本实现
服务器实现由无限循环组成,其中
选择器
等待事件并创建事件键。密钥有四种可能的类型:通常
可接受的
密钥是在服务器端创建的。事实上,这种密钥只是通知服务器客户端需要连接,然后服务器将套接字通道单独化并将其与选择器关联以进行读/写操作。此后,当接受的客户端读取或写入某些内容时,选择器将为该客户端创建可读
或可写
密钥。现在您已准备好用Java编写服务器,遵循所提出的算法。套接字通道、
选择器
的创建以及套接字选择器注册可以通过以下方式进行:首先,我们使用
ServerSocketChannel 创建一个
SocketChannel
实例。 open() 方法。接下来,configureBlocking(false)
调用将此通道
设置为非阻塞。与服务器的连接是通过serverChannel.socket().bind()
方法建立的。HOSTNAME
代表服务器的IP地址,PORT
是通信端口。最后,调用Selector.open()
方法创建一个selector
实例并将其注册到channel
和注册类型。在此示例中,注册类型为 OP_ACCEPT,这意味着选择器仅报告客户端尝试连接到服务器。其他可能的选项有:OP_CONNECT
,它将由客户端使用; OP_READ;和OP_WRITE
。现在我们需要使用无限循环来处理这个请求。一个简单的方法如下:
您可以找到 这里是实现源
注意:异步服务器
作为非阻塞实现的替代方案,我们可以部署异步服务器。例如,您可以使用 AsynchronousServerSocketChannel 类,它为面向流的侦听套接字提供异步通道。
要使用它,首先执行其静态
open()
方法,然后将其bind()
到特定的端口。接下来,您将执行其accept()
方法,并向其传递一个实现CompletionHandler
接口的类。大多数情况下,您会发现该处理程序被创建为匿名内部类。从此
AsynchronousServerSocketChannel
对象中,您调用accept()
来告诉它开始侦听连接,并向其传递一个自定义CompletionHandler
实例。当我们调用accept()时,它会立即返回。请注意,这与传统的阻塞方法不同;而accept()
方法会被阻止,直到客户端连接到它,而AsynchronousServerSocketChannel
accept()
方法会处理它为你。这里有一个示例:
您可以在此处找到完整代码
Java non-blocking sockets were introduced in Java 2 Standard Edition 1.4. It allows net communication between applications using the socket without blocking the processes. But Teo, what is a non-blocking socket?, in which contexts can it be useful?, and how does it work? Okay young Padawan, let's answer these questions.
What is a non-blocking socket?
A non-blocking socket allows I/O operations on a channel without blocking the processes using it. This means, we can use a single thread to handle multiple concurrent connections and gain "asynchronous high-performance" read/write operations (some people may not agreed with that).
Ok, in which contexts can it be useful?
Suppose you would like to implement a server accepting diverse client connections. Suppose, as well, that you would like the server to be able to process multiple requests simultaneously. Using the traditional way you have two choices to develop such a server:
a. Implement a multi-threaded server that manually handles a thread for each connection.
b. Using an external third-party module.
Both solutions work, but adopting the first one you have to develop the whole thread-management solution, with related concurrency and conflict troubles. The second solution makes the application dependent on a non-JDK external module and probably you have to adapt the library to your necessities. By means of the non-blocking socket, you can implement a non-blocking server without directly managing threads or resorting to external modules.
How it works?
Before going into details, there are few terms that you need to understand:
Java NIO has a class called
Selector
that allows a single thread to examine I/O events on multiple channels. How is this possible? Well, theselector
can check the "readiness" of a channel for events such as a client attempting a connection, or a read/write operation. This is, each instance ofSelector
can monitor more socket channels and thus more connections. Now, when something happens on the channel (an event occurs), theselector
informs the application to process the request. Theselector
does it by creating event keys (or selection keys), which are instances of theSelectionKey
class. Eachkey
holds information about who is making the request and what type of the request is, as shown in the Figure 1.Figure 1: Structure diagram
A basic implementation
A server implementation consists of an infinite loop in which the
selector
waits for events and creates the event keys. There are four possible types for a key:Usually
acceptable
keys are created on the server side. In fact, this kind of key simply informs the server that a client required a connection, then the server individuates the socket channel and associates this to the selector for read/write operations. After this, when the accepted client reads or writes something, the selector will createreadable
orwriteable
keys for that client..Now you are ready to write the server in Java, following the proposed algorithm. The creation of the socket channel, the
selector
, and the socket-selector registration can be made in this way:First we create an instance of
SocketChannel
withServerSocketChannel.open()
method. Next,configureBlocking(false)
invocation sets thischannel
as nonblocking. The connection to the server is made byserverChannel.socket().bind()
method. TheHOSTNAME
represents the IP address of the server, andPORT
is the communication port. Finally, invokeSelector.open()
method to create aselector
instance and register it to thechannel
and registration type. In this example, the registration type isOP_ACCEPT
, which means the selector merely reports that a client attempts a connection to the server. Other possible options are:OP_CONNECT
, which will be used by the client;OP_READ
; andOP_WRITE
.Now we need to handle this requests using an infinite loop. A simple way is the following:
You can find the implementation source here
NOTE: Asynchronous Server
An alternative to the the Non-blocking implementation we can deploy an Asynchronous Server. For instance, you can use the
AsynchronousServerSocketChannel
class, which provides an asynchronous channel for stream-oriented listening sockets.To use it, first execute its static
open()
method and thenbind()
it to a specific port. Next, you'll execute itsaccept()
method, passing to it a class that implements theCompletionHandler
interface. Most often, you'll find that handler created as an anonymous inner class.From this
AsynchronousServerSocketChannel
object, you invokeaccept()
to tell it to start listening for connections, passing to it a customCompletionHandler
instance. When we invokeaccept()
, it returns immediately. Note that this is different from the traditional blocking approach; whereas theaccept()
method blocked until a client connected to it, theAsynchronousServerSocketChannel
accept()
method handles it for you.Here you have an example:
You can find the full code here
只有一种方法。
SocketChannel.configureBlocking(false)
。请注意,其中一些答案是不正确的。 SocketChannel。 configureBlocking(false) 将其置于非阻塞模式。您不需要
选择器
来执行此操作。您只需要一个选择器
即可通过非阻塞套接字实现超时或多路复用 I/O。There is only one way.
SocketChannel.configureBlocking(false)
.Note that several of these answers are incorrect. SocketChannel.configureBlocking(false) puts it into non-blocking mode. You don't need a
Selector
to do that. You only need aSelector
to implement timeouts or multiplexed I/O with non-blocking sockets.除了使用非阻塞 IO 之外,您可能会发现为连接提供一个写入线程要简单得多。
注意:如果您只需要几千个连接,每个连接一到两个线程会更简单。如果每台服务器有大约一万个或更多连接,则需要带有选择器的 NIO。
Apart from using non blocking IO, you might find it is much simpler to have a writing thread for your connection.
Note: if you only need a few thousand connections, one to two threads per connection is simpler. If you have around ten thousand or more connections per server you need NIO with Selectors.
java.nio 包提供了选择器,其工作方式与 C 中非常相似。
java.nio package provides Selector working much like as in C.
我刚刚写了这段代码。效果很好。这是上面答案中提到的 Java NIO 的示例,但在这里我发布了代码。
I just wrote this code . It works well . This is an example of the Java NIO as mentioned in the above answers but here i post the code .