- 浏览: 950790 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Channel接口定义:http://donald-draper.iteye.com/blog/2369111
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
先来回顾一下ServerSocketChannel继承结构树:
今天来看一下ServerSocketChannel的定义
从上面可以看出ServerSocketChannel主要是绑定socket地址,监听Socket连接。
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
先来回顾一下ServerSocketChannel继承结构树:
//ServerSocketChannel public abstract class ServerSocketChannel extends AbstractSelectableChannel implements NetworkChannel //AbstractSelectableChannel public abstract class AbstractSelectableChannel extends SelectableChannel //SelectableChannel public abstract class SelectableChannel extends AbstractInterruptibleChannel implements Channel
今天来看一下ServerSocketChannel的定义
package java.nio.channels; import java.io.IOException; import java.net.ServerSocket; import java.net.SocketOption; import java.net.SocketAddress; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; /** * A selectable channel for stream-oriented listening sockets. *面向流监听socket可选择通道。 * A server-socket channel is created by invoking the {@link #open() open} * method of this class. It is not possible to create a channel for an arbitrary, * pre-existing {@link ServerSocket}. A newly-created server-socket channel is * open but not yet bound. An attempt to invoke the {@link #accept() accept} * method of an unbound server-socket channel will cause a {@link NotYetBoundException} * to be thrown. A server-socket channel can be bound by invoking one of the * {@link #bind(java.net.SocketAddress,int) bind} methods defined by this class. *ServerSocketChannel通过open方法,创建一个ServerSocketChannel通道。如果已经存在一个 ServerSocket,将不会创建ServerSocketChannel。新创建的ServerSocketChannel是没有绑定地址的。 调用没有绑定地址的ServerSocketChannel的accept方法,将会抛出NotYetBoundException。 可以通过bind方法绑定地址。 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object) * setOption} method. Server-socket channels support the following options: 可用#setOption方法,配置socket选项,ServerSocketChannel支持的选项配置如下: * <blockquote> * <table border> * <tr> * <th>Option Name</th> * <th>Description</th> * </tr> * <tr>接受缓冲区大小 * <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td> //StandardSocketOptions * <td> The size of the socket receive buffer </td> public static final SocketOption<Integer> SO_RCVBUF = new StdSocketOption<Integer>("SO_RCVBUF", Integer.class); * </tr> * <tr>是否可以重用地址 * <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td> * <td> Re-use address </td> //StandardSocketOptions public static final SocketOption<Boolean> SO_REUSEADDR = new StdSocketOption<Boolean>("SO_REUSEADDR", Boolean.class); * </tr> * </table> * </blockquote> * Additional (implementation specific) options may also be supported. * * <p> Server-socket channels are safe for use by multiple concurrent threads. ServerSocketChannel是线程安全的 * * * @author Mark Reinhold * @author JSR-51 Expert Group * @since 1.4 */ public abstract class ServerSocketChannel extends AbstractSelectableChannel implements NetworkChannel { /** * Initializes a new instance of this class. */ protected ServerSocketChannel(SelectorProvider provider) { super(provider); } /** * Opens a server-socket channel. *打开一个ServerSocketChannel * The new channel is created by invoking the {@link * java.nio.channels.spi.SelectorProvider#openServerSocketChannel * openServerSocketChannel} method of the system-wide default {@link * java.nio.channels.spi.SelectorProvider} object. *通过系统默认的SelectorProvider实现创建一个ServerSocketChannel * <p> The new channel's socket is initially unbound; it must be bound to a * specific address via one of its socket's {@link * java.net.ServerSocket#bind(SocketAddress) bind} methods before * connections can be accepted. *新创建的ServerSocketChannel初始是没有绑定的,在接受连接之前必须绑定一个SocketAddress * @return A new socket channel * * @throws IOException * If an I/O error occurs */ public static ServerSocketChannel open() throws IOException { return SelectorProvider.provider().openServerSocketChannel(); } /** * Returns an operation set identifying this channel's supported * operations. *返回通道支持的操作事件集 * Server-socket channels only support the accepting of new * connections, so this method returns {@link SelectionKey#OP_ACCEPT}. ServerSocketChannel只支持接受连接操作事件 * * * @return The valid-operation set */ public final int validOps() { return SelectionKey.OP_ACCEPT; } // -- ServerSocket-specific operations -- /** * Binds the channel's socket to a local address and configures the socket * to listen for connections. *绑定ServerSocketChannel到本地的socket地址,并配置socket监听连接, 具体实现看bind(SocketAddress local, int backlog)方法 * An invocation of this method is equivalent to the following: * <blockquote><pre> * bind(local, 0); * </pre></blockquote> * * @param local * The local address to bind the socket, or {@code null} to bind * to an automatically assigned socket address * * @return This channel * * @throws AlreadyBoundException {@inheritDoc} * @throws UnsupportedAddressTypeException {@inheritDoc} * @throws ClosedChannelException {@inheritDoc} * @throws IOException {@inheritDoc} * @throws SecurityException * If a security manager has been installed and its {@link * SecurityManager#checkListen checkListen} method denies the * operation * * @since 1.7 */ public final ServerSocketChannel bind(SocketAddress local) throws IOException { //委托给bind(SocketAddress local, int backlog) return bind(local, 0); } /** * Binds the channel's socket to a local address and configures the socket to * listen for connections. *绑定ServerSocketChannel到本地的socket地址,并配置socket监听连接 * <p> This method is used to establish an association between the socket and * a local address. Once an association is established then the socket remains * bound until the channel is closed. *这个方法用于建立socket与本地socket地址的联系。只要联系建立,在通道关闭之前,socket 能绑定着地址。 * <p> The {@code backlog} parameter is the maximum number of pending * connections on the socket. Its exact semantics are implementation specific. * In particular, an implementation may impose a maximum length or may choose * to ignore the parameter altogther. If the {@code backlog} parameter has * the value {@code 0}, or a negative value, then an implementation specific * default is used. *backlog参数是socket运行接受的最大连接数。具体的含义要根据具体的实现,有的是最大连接数, 有的可能忽略这个参数。如果参数为0或者一个负数,将会使用默认的实现机制 * @param local 本地socket地址 * The address to bind the socket, or {@code null} to bind to an * automatically assigned socket address * @param backlog 最大连接数 * The maximum number of pending connections * * @return This channel * * @throws AlreadyBoundException 地址已经绑定 * If the socket is already bound * @throws UnsupportedAddressTypeException 地址不支持 * If the type of the given address is not supported * @throws ClosedChannelException 通道关闭 * If this channel is closed * @throws IOException IO操作异常 * If some other I/O error occurs * @throws SecurityException * If a security manager has been installed and its {@link * SecurityManager#checkListen checkListen} method denies the * operation * * @since 1.7 */ public abstract ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException; /** * @throws UnsupportedOperationException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @throws ClosedChannelException {@inheritDoc} * @throws IOException {@inheritDoc} *设置选项 * @since 1.7 */ public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException; /** * Retrieves a server socket associated with this channel. *返回与通道关联的ServerSocket * <p> The returned object will not declare any public methods that are not * declared in the {@link java.net.ServerSocket} class. * * @return A server socket associated with this channel */ public abstract ServerSocket socket(); /** * Accepts a connection made to this channel's socket. * 接受一个连接到通道socket的请求。 * If this channel is in non-blocking mode then this method will * immediately return <tt>null</tt> if there are no pending connections. * Otherwise it will block indefinitely until a new connection is available * or an I/O error occurs. *如果通道是非阻塞模式,,如果连接已经达到最大数量,则立即返回null; 否则不确定地阻塞直到一个新的连接可用或者IO错误发生 * <p> The socket channel returned by this method, if any, will be in * blocking mode regardless of the blocking mode of this channel. * 如果通道是阻塞模式,则将返回一个SocketChannle,并忽略阻塞模式 * <p> This method performs exactly the same security checks as the {@link * java.net.ServerSocket#accept accept} method of the {@link * java.net.ServerSocket} class. That is, if a security manager has been * installed then for each new connection this method verifies that the * address and port number of the connection's remote endpoint are * permitted by the security manager's {@link * java.lang.SecurityManager#checkAccept checkAccept} method. *此方执行精确的安全检查 * @return The socket channel for the new connection, * or <tt>null</tt> if this channel is in non-blocking mode * and no connection is available to be accepted * * @throws ClosedChannelException * If this channel is closed * * @throws AsynchronousCloseException * If another thread closes this channel * while the accept operation is in progress * * @throws ClosedByInterruptException 如果其他线程中断当前线程,则关闭通道,设置当前线程的中断位。 * If another thread interrupts the current thread * while the accept operation is in progress, thereby * closing the channel and setting the current thread's * interrupt status * * @throws NotYetBoundException 地址没有绑定 * If this channel's socket has not yet been bound * * @throws SecurityException 安全异常 * If a security manager has been installed * and it does not permit access to the remote endpoint * of the new connection * * @throws IOException IO操作异常 * If some other I/O error occurs */ public abstract SocketChannel accept() throws IOException; }
从上面可以看出ServerSocketChannel主要是绑定socket地址,监听Socket连接。
发表评论
-
文件通道解析二(文件锁,关闭通道)
2017-05-16 23:17 992文件通道解析一(读写操作,通道数据传输等):http://do ... -
文件通道解析一(读写操作,通道数据传输等)
2017-05-16 10:04 1101Reference定义(PhantomRefere ... -
文件通道创建方式综述
2017-05-15 17:39 931Reference定义(PhantomReference,Cl ... -
文件读写方式简单综述后续(文件,流构造)
2017-05-14 23:04 1399Java Socket通信实例:http://donald-d ... -
文件读写方式简单综述
2017-05-14 11:13 1068Java Socket通信实例:http://donald-d ... -
FileChanne定义
2017-05-12 23:28 869文件读写方式简单综述:http://donald-draper ... -
SeekableByteChannel接口定义
2017-05-11 08:43 1121ByteChannel,分散聚集通道接口的定义(SocketC ... -
FileChannel示例
2017-05-11 08:37 906前面我们看过socket通道,datagram通道,以管道Pi ... -
PipeImpl解析
2017-05-11 08:41 837ServerSocketChannel定义:http://do ... -
Pipe定义
2017-05-10 09:07 839Channel接口定义:http://donald-drape ... -
NIO-Pipe示例
2017-05-10 08:47 846PipeImpl解析:http://donald-draper ... -
DatagramChannelImpl 解析四(地址绑定,关闭通道等)
2017-05-10 08:27 690DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析三(多播)
2017-05-10 08:20 1687DatagramChannelImpl 解析一(初始化):ht ... -
NIO-UDP实例
2017-05-09 12:32 1516DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析二(报文发送与接收)
2017-05-09 09:03 1341DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析一(初始化)
2017-05-08 21:52 1329Channel接口定义:http://donald-drape ... -
MembershipKeyImpl 简介
2017-05-08 09:11 871MembershipKey定义:http://donald-d ... -
DatagramChannel定义
2017-05-07 23:13 1182Channel接口定义:http://donald-drape ... -
MulticastChanne接口定义
2017-05-07 13:45 1067NetworkChannel接口定义:ht ... -
MembershipKey定义
2017-05-06 16:20 829package java.nio.channels; i ...
相关推荐
NIO(服务端和客户端代码) 博文链接:https://songjianyong.iteye.com/blog/1757406
java的ServerSocketChannel与SocketChannel的使用
一个关于SocketChannel、ServerSocketChannel、Selector的综合案例
java api之ServerSocketChannel基础、应用场景、实战讲解
jdk api-ServerSocketChannel、Selector、ByteBuffer结合实现网络报文间的通讯
【IT十八掌徐培成】Java基础第27天-02.NIO-ServerSocketChannel-SocketChannel.zip
● ServerSocketChannel:ServerSocket的替代类,支持阻塞通信与非阻塞通信。 ● SocketChannel:Socket的替代类,支持阻塞通信与非阻塞通信。 ● Selector:为ServerSocketChannel监控接收连接就绪事件,为...
The networking capabilities of the Java platform have been extended considerably since the first edition of the book. This new edition covers version 1.5-1.7, the most current iterations, as well as ...
NIO SSL 与阻塞IO不同,JVM不提供扩展基本套接字通道类的标准SSLSocketChannel和SSLServerSocketChannel类。 相反,必须使用手动编排SSL交换。 该项目提供了和,可以像和一样使用。入门直接下载您可以直接下载并将其...
Java中的NIO其实是NewIO(另一种解释是NO-blocking IO,确实,在Java中可以通过ServerSocketChannel实现非双向IO) 注意:ServerSocketChannel至少是一对的,所以可以代替JDK1.4之前的BIO(ServerSocket) 其实Java...
1.ServerSocketChannel与SocketChannel通讯 2.java Fork/Join 与ThreadPool使用
Java NIO系列教程(一) Java NIO 概述 ...Java NIO系列教程(九) ServerSocketChannel Java NIO系列教程(十) Java NIO DatagramChannel Java NIO系列教程(十一) Pipe Java NIO系列教程(十二) Java NIO与IO
1) 创建ServerSocketChannel,并设置相应参数 2) 创建Selector并注册到ServerSocketChannel上 3) 调用Selector的select方法等待请求 4) Selector接收到请求后使用selectedKeys返回SelectionKey集合 5) ...
流程走到了启动好reactor线程后,ServerSocketChannel注册到selector上,但是感兴趣事件填的0,我们继续跟流程,走到这里,initAndRegister方法完成,继续跟bind方法,我们看bind方法中的doBind0方法 /** * * @...
非阻塞io案列分析,多个buffer的聚合和分散应用,通道、缓冲、buffer 文件的读写 ServerSocketChannel 、 SocketChannel ,
08-Java NIO-Channel-ServerSocketChannel.mp4 09-Java NIO-Channel-SocketChannel.mp4 10-Java NIO-Channel-DatagramChannel.mp4 11-Java NIO-Channel-分散和聚集.mp4 12-Java NIO-Buffer-概述.mp4 13-Java NIO-...
08-Java NIO-Channel-ServerSocketChannel.mp4 09-Java NIO-Channel-SocketChannel.mp4 10-Java NIO-Channel-DatagramChannel.mp4 11-Java NIO-Channel-分散和聚集.mp4 12-Java NIO-Buffer-概述.mp4 13-Java NIO-...
(3), 在这里,我们指定使用NioServerSocketChannel类,该类用于实例化新的Channel以接受传入的连接。 (4),保持长连接 (5), childHandler()方法需要一个ChannelInitializer类,ChannelInitializer是一个特殊的处理...
如果你至今还是在怀疑Java的性能,说明你的思想和观念已经完全落伍了,Java一两年就应该用新的名词来定义。从JDK1.5开始又要提供关于线程、并发等新性能的支持,Java应用在游戏等适时领域方面的机会已经成熟,Java在...
ServerSocketChannel注册 在完成channel的创建以及初始化之后,接下来进行channel的注册过程。 先看下config(): config是ServerBootstrap的成员变量实例ServerBootstrapConfig。ServerBootstrap在前面的准备工作...