(已完成)java nio-channel.md

282 阅读2分钟

类继承图

1.接口Channel

2.抽象类
服务器端ServerSocketChannel和客户端SocketChannel
与socket平级的通道还有本地磁盘文件file 网络套接字socket device 内存等。也就是说 通道不止一种 其中有包括了套接字socket。nio包的channel只是把不同硬件设备(网络编程-网卡 磁盘文件 内存)的读写方式 统一成了通道这么一个概念。具体来说 就是 不同通道都继承了Channel这个接口。

3.实现可
服务器端ServerSocketChannelImpl和客户端SocketChannelImpl

file:///var/folders/m9/yshd99dx1g38gtyxvv3k201r0000gn/T/WizNote/66d37a06-db36-4885-97c7-d3a782a18a83/index_files/d6d53fd2-73fb-4416-a4a4-8f0e8bdd3bdb.jpg

file:///var/folders/m9/yshd99dx1g38gtyxvv3k201r0000gn/T/WizNote/66d37a06-db36-4885-97c7-d3a782a18a83/index_files/39568da6-b31d-491d-bdfe-b6e4d4eae272.jpg

服务器端

主要方法
1.bind()
2.listen()
3.accept()方法 //接受客户端的连接

4.读写数据

时序图

file:///var/folders/m9/yshd99dx1g38gtyxvv3k201r0000gn/T/WizNote/66d37a06-db36-4885-97c7-d3a782a18a83/index_files/02a75fd2-efa3-4b2b-ba48-92a5e7f8353d.png

//流程图

源码分析

ServerSocketChannelImpl

/**

 * An implementation of ServerSocketChannels

 */



class ServerSocketChannelImpl

    extends ServerSocketChannel

    implements SelChImpl

{



// Our socket adaptor, if any

    ServerSocket socket; //这里虽然包含了




public ServerSocket socket() {

        synchronized (stateLock) {

            if (socket == null)

                socket = ServerSocketAdaptor.create(this);

            return socket;

        }

    }



@Override

    public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {

        synchronized (lock) {

            if (!isOpen())

                throw new ClosedChannelException();

            if (isBound())

                throw new AlreadyBoundException();

            InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) :

                Net.checkAddress(local);

            SecurityManager sm = System.getSecurityManager();

            if (sm != null)

                sm.checkListen(isa.getPort());

            NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());

            Net.bind(fd, isa.getAddress(), isa.getPort()); //native方法

            Net.listen(fd, backlog < 1 ? 50 : backlog); //native方法

            synchronized (stateLock) {

                localAddress = Net.localAddress(fd);

            }

        }

        return this;

    }




public class ServerSocketAdaptor                        // package-private

    extends ServerSocket

{



    // The channel being adapted

    private final ServerSocketChannelImpl ssc;


public void bind(SocketAddress local, int backlog) throws IOException {

        if (local == null)

            local = new InetSocketAddress(0);

        try {

            ssc.bind(local, backlog); //最终还是调用ServerSocketChannelImpl.bind()

        } catch (Exception x) {

            Net.translateException(x);

        }

    }


ServerSocketChannel

/**

 * A selectable channel for stream-oriented listening sockets.

 *

 * <p> 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.

 *

 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)

 * setOption} method. Server-socket channels support the following options:

 * <blockquote>

 * <table border summary="Socket options">

 *   <tr>

 *     <th>Option Name</th>

 *     <th>Description</th>

 *   </tr>

 *   <tr>

 *     <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td>

 *     <td> The size of the socket receive buffer </td>

 *   </tr>

 *   <tr>

 *     <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>

 *     <td> Re-use address </td>

 *   </tr>

 * </table>

 * </blockquote>

 * Additional (implementation specific) options may also be supported.

 *

 * <p> Server-socket channels are safe for use by multiple concurrent threads.

 * </p>

 *

 * @author Mark Reinhold

 * @author JSR-51 Expert Group

 * @since 1.4

 */



public abstract class ServerSocketChannel

    extends AbstractSelectableChannel

    implements NetworkChannel

{


public abstract class SelectableChannel

    extends AbstractInterruptibleChannel

    implements Channel

{


客户端

主要方法
1.connect()方法 //连接服务器端

2.读写数据