一、ServerSocketChannel
1), ServerSocketChannel monitors new client Socket connections on the server side
2)、public abstract class ServerSocketChannel extends AbstractSelectableChannel implements NetworkChannel
3), related methods
method | description |
---|---|
public static ServerSocketChannel open() | Get a ServerSocketChannel channel |
public final ServerSocketChannel bind(SocketAddress local) | Set the server port number |
public final SelectableChannel configureBlocking(boolean block) | Set blocking or non-blocking mode, set false to use non-blocking mode |
public abstract SocketChannel accept() | Accept a connection and return the channel object representing this connection |
public final SelectionKey register(Selector sel, int ops) | Register a selector and set up listening events |
二、SocketChannel
1), SocketChannel, network IO channel, specifically responsible for read and write operations . NIO writes the data in the buffer to the channel or reads the data in the channel to the buffer
2)、public abstract class SocketChannel extends AbstractSelectableChannel
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel
3), related methods
method | description |
---|---|
public static SocketChannel open() | Get a SocketChannel channel |
public final SelectableChannel configureBlocking(boolean block) | Set blocking or non-blocking mode, set false to use non-blocking mode |
public abstract boolean connect(SocketAddress remote) | connect to the server |
public abstract boolean finishConnect() | If the connect method fails to connect, then the connection operation must be completed through the finishConnect method |
public abstract int write(ByteBuffer src) | Write data to the channel |
public abstract int read(ByteBuffer dst) | Read data from the channel |
public final SelectionKey register(Selector sel,int ops,Object att) | Register a selector and set listening events, the last parameter can be set to share data |
public final void close() | Close the channel |