`
Donald_Draper
  • 浏览: 981569 次
社区版块
存档分类
最新评论

SocketChannel接口定义

    博客分类:
  • NIO
nio 
阅读更多
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定义:http://donald-draper.iteye.com/blog/2369836
ServerSocketChannelImpl解析:http://donald-draper.iteye.com/blog/2370912
Selector定义:http://donald-draper.iteye.com/blog/2370015
AbstractSelector定义:http://donald-draper.iteye.com/blog/2370138
SelectorImpl分析 :http://donald-draper.iteye.com/blog/2370519
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper):
http://donald-draper.iteye.com/blog/2370811
WindowsSelectorImpl解析二(选择操作,通道注册,通道反注册,选择器关闭等):
http://donald-draper.iteye.com/blog/2370862
ByteChannel,分散聚集通道接口的定义(SocketChannel):
http://donald-draper.iteye.com/blog/2371065
前面一篇文章我们看了一个socket通道继承的接口 ByteChannel, ByteChannel主要是继承了
可读(ReadableByteChannel)可写(WritableByteChannel)通道接口和分散(ScatteringByteChannel)聚集(ScatteringByteChannel)通道接口;可读通道接口,可以从通道读取字节序列写到缓存区;可写通道接口,可以从缓存区读取字节序列写到通道;分散通道可以从通道读取字节序列,写到一组缓存区中,聚集通道可以从一组缓存区读取字节序列,写到通道。今天我们就来看下SocketChannel接口的定义。
package java.nio.channels;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketOption;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.SelectorProvider;

/**
 * A selectable channel for stream-oriented connecting sockets.
 *SocketChannel是一个面向流的连接socket可选通道。
 *  A socket channel is created by invoking one of the {@link #open open}
 * methods of this class.  It is not possible to create a channel for an arbitrary,
 * pre-existing socket. A newly-created socket channel is open but not yet
 * connected.  An attempt to invoke an I/O operation upon an unconnected
 * channel will cause a {@link NotYetConnectedException} to be thrown.  A
 * socket channel can be connected by invoking its {@link #connect connect}
 * method; once connected, a socket channel remains connected until it is
 * closed.  Whether or not a socket channel is connected may be determined by
 * invoking its {@link #isConnected isConnected} method.
 *socket通道调用#open方法创建一个socket通道。如果socket已经存在,则不可能强制创建一个通道。
 一个新创建的socket通道处于打开状态,但还没有连接。在一个没有连接的通道,调用IO操作,将会引起
 一个NotYetConnectedException抛出。socket通道可以调用#connect方法连接一个socket地址;
 一旦建立连接,在连接关闭之前,通道都是可连接的。我们可以通道#isConnected方法,判断一个socket通道
 是否建立连接。
 *  A socket
 * channel may be created and the process of establishing the link to the
 * remote socket may be initiated via the {@link #connect connect} method for
 * later completion by the {@link #finishConnect finishConnect} method.
 * Whether or not a connection operation is in progress may be determined by
 * invoking the {@link #isConnectionPending isConnectionPending} method.
 *socket通道支持非阻塞模式连接;一个socket通道已经建立的情况下,调用#connect方法
 初始化建立远端socket过程,之后通过#finishConnect完成连接。一个连接操作是否正在进行,
 可以通过#isConnectionPending方法判断。
 * <p> Socket channels support <i>asynchronous shutdown,</i> which is similar
 * to the asynchronous close operation specified in the {@link Channel} class.
 * If the input side of a socket is shut down by one thread while another
 * thread is blocked in a read operation on the socket's channel, then the read
 * operation in the blocked thread will complete without reading any bytes and
 * will return <tt>-1</tt>.  If the output side of a socket is shut down by one
 * thread while another thread is blocked in a write operation on the socket's
 * channel, then the blocked thread will receive an {@link
 * AsynchronousCloseException}.
 *socket通道支持异步关闭,这个与Channel的关闭操作相似。如果其他线程阻塞在通道的读操作上,
 一个线程关闭了socket的输入流,阻塞的读操作线程将会返回-1,即没有读取任何字节。
 如果其他线程阻塞在通道的写操作上, 一个线程关闭了socket的输出流,
 阻塞的写操作线程将会将会接受到一个AsynchronousCloseException。
 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
 * setOption} method. Socket channels support the following options:
 * <blockquote>
 * <table border>
 *   <tr>Socket的选项配置通过setOption(SocketOption,Object)方法,Socket channels
 支持一下选项配置。
 *     <th>Option Name</th>
 *     <th>Description</th>
 *   </tr>
 *   <tr>发送缓存区大小
 *     <td> {@link java.net.StandardSocketOptions#SO_SNDBUF SO_SNDBUF} </td>
 *     <td> The size of the socket send buffer </td>
 *   </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_KEEPALIVE SO_KEEPALIVE} </td>
 *     <td> Keep connection alive </td>
 *   </tr>
 *   <tr>地址是否可以重用
 *     <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>
 *     <td> Re-use address </td>
 *   </tr>
 *   <tr>在通道阻塞模式下,再通道关闭时,如果socket输出流还有数据没发送,则延时等待
 数据发送。
 *     <td> {@link java.net.StandardSocketOptions#SO_LINGER SO_LINGER} </td>
 *     <td> Linger on close if data is present (when configured in blocking mode
 *          only) </td>
 *   </tr>
 *   <tr>TCP延时,Disable the Nagle algorithm.具体可以在StandardSocketOptions中查看
 *     <td> {@link java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY} </td>
 *     <td> Disable the Nagle algorithm </td>
 *   </tr>
 * </table>
 * </blockquote>
 * Additional (implementation specific) options may also be supported.
 *
 * <p> Socket channels are safe for use by multiple concurrent threads.  They
 * support concurrent reading and writing, though at most one thread may be
 * reading and at most one thread may be writing at any given time.  The {@link
 * #connect connect} and {@link #finishConnect finishConnect} methods are
 * mutually synchronized against each other, and an attempt to initiate a read
 * or write operation while an invocation of one of these methods is in
 * progress will block until that invocation is complete.  

 *多线程可以安全地并发访问时socket通道。尽管至多只有一个线程进行读写操作,socket通道
 支持并发的读写。#connect和#finishConnect是手动synchronized,在#connect和#finishConnect方法
 调用的过程中,尝试读写操作,将会阻塞至#connect和#finishConnect方法结束。
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public abstract class SocketChannel
    extends AbstractSelectableChannel
    implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel
{
/**
     * Initializes a new instance of this class.
     */
    protected SocketChannel(SelectorProvider provider) {
        super(provider);
    }

    /**
     * Opens a socket channel.
     *打开一个通道
     *  The new channel is created by invoking the {@link
     * java.nio.channels.spi.SelectorProvider#openSocketChannel
     * openSocketChannel} method of the system-wide default {@link
     * java.nio.channels.spi.SelectorProvider} object.  

     *一个通道通过默认的SelectorProvider的openSocketChannel创建
     * @return  A new socket channel
     *
     * @throws  IOException
     *          If an I/O error occurs
     */
    public static SocketChannel open() throws IOException {
        return SelectorProvider.provider().openSocketChannel();
    }

    /**
     * Opens a socket channel and connects it to a remote address.
     *打开一个通道,并连接一个远程地址
     *  This convenience method works as if by invoking the {@link #open()}
     * method, invoking the {@link #connect(SocketAddress) connect} method upon
     * the resulting socket channel, passing it <tt>remote</tt>, and then
     * returning that channel.  

     *此方法与open方法基本相同,只是在创建SocketChannel后,调用connect(SocketAddress)
     连接远程地址
     * @param  remote
     *         The remote address to which the new channel is to be connected
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the connect operation is in progress
     *
     * @throws  ClosedByInterruptException
     *          If another thread interrupts the current thread
     *          while the connect operation is in progress, thereby
     *          closing the channel and setting the current thread's
     *          interrupt status
     *
     * @throws  UnresolvedAddressException
     *          If the given remote address is not fully resolved
     *
     * @throws  UnsupportedAddressTypeException
     *          If the type of the given remote address is not supported
     *
     * @throws  SecurityException
     *          If a security manager has been installed
     *          and it does not permit access to the given remote endpoint
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public static SocketChannel open(SocketAddress remote)
        throws IOException
    {
        SocketChannel sc = open();
        try {
	    //连接远程地址
            sc.connect(remote);
        } catch (Throwable x) {
            try {
                sc.close();
            } catch (Throwable suppressed) {
	        //当一个异常被抛出的时候,可能有其他异常因为该异常而被抑制住,
		//从而无法正常抛出。这时可以通过Java 7中为Throwable类增加的addSuppressed方法把这些被抑制的方法记录下来。
		//被抑制的异常会出现在抛出的异常的堆栈信息中,
		//也可以通过getSuppressed方法来获取这些异常。这样做的好处是不会丢失任何异常,方便开发人员进行调试。
                x.addSuppressed(suppressed);
            }
            throw x;
        }
	//断言是否连接
        assert sc.isConnected();
        return sc;
    }

    /**
     * Returns an operation set identifying this channel's supported
     * operations.
     *返回通道支持的操作事件集,通道支持读写连接操作事件
     *  Socket channels support connecting, reading, and writing, so this
     * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
     * <tt>|</tt> {@link SelectionKey#OP_READ} <tt>|</tt> {@link
     * SelectionKey#OP_WRITE}<tt>)</tt>.  

     *
     * @return  The valid-operation set
     */
    public final int validOps() {
        return (SelectionKey.OP_READ
                | SelectionKey.OP_WRITE
                | SelectionKey.OP_CONNECT);
    }


    // -- Socket-specific operations --

    /**
     * @throws  ConnectionPendingException
     *          If a non-blocking connect operation is already in progress on
     *          this channel
     * @throws  AlreadyBoundException               {@inheritDoc}
     * @throws  UnsupportedAddressTypeException     {@inheritDoc}
     * @throws  ClosedChannelException              {@inheritDoc}
     * @throws  IOException                         {@inheritDoc}
     *地址绑定
     * @since 1.7
     */
    @Override
    public abstract SocketChannel bind(SocketAddress local)
        throws IOException;

    /**
     * @throws  UnsupportedOperationException           {@inheritDoc}
     * @throws  IllegalArgumentException                {@inheritDoc}
     * @throws  ClosedChannelException                  {@inheritDoc}
     * @throws  IOException                             {@inheritDoc}
     *数字socket通道选项配置
     * @since 1.7
     */
    @Override
    public abstract <T> SocketChannel setOption(SocketOption<T> name, T value)
        throws IOException;

    /**
     * Shutdown the connection for reading without closing the channel.
     *在没有关闭通道的情况下,关闭读操作连接,即不能从通道读取数据,写到缓冲区。
     *  Once shutdown for reading then further reads on the channel will
     * return {@code -1}, the end-of-stream indication. If the input side of the
     * connection is already shutdown then invoking this method has no effect.
     *读连接关闭,进一步的读操作将会返回-1,表示读到流的末端。如果连接的输入流已经
     关闭,调用此方法无效。
     * @return  The channel
     *
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     * @throws  ClosedChannelException
     *          If this channel is closed
     * @throws  IOException
     *          If some other I/O error occurs
     *
     * @since 1.7
     */
    public abstract SocketChannel shutdownInput() throws IOException;

    /**
     * Shutdown the connection for writing without closing the channel.
     *在没有关闭通道的情况下,关闭到通道的写操作连接
     * <p> Once shutdown for writing then further attempts to write to the
     * channel will throw {@link ClosedChannelException}. If the output side of
     * the connection is already shutdown then invoking this method has no
     * effect.
     *在写连接关闭时,进一步尝试从缓冲区读数据,写到通道,将会抛出ClosedChannelException。
     如果连接的输出流关闭,则调用此方法无效。
     * @return  The channel
     *
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     * @throws  ClosedChannelException
     *          If this channel is closed
     * @throws  IOException
     *          If some other I/O error occurs
     *
     * @since 1.7
     */
    public abstract SocketChannel shutdownOutput() throws IOException;

    /**
     * Retrieves a socket associated with this channel.
     *获取与通道关联的socket
     * <p> The returned object will not declare any public methods that are not
     * declared in the {@link java.net.Socket} class.  

     *返回的socket不会声明任何在socket中没有声明的public方法
     * @return  A socket associated with this channel
     */
    public abstract Socket socket();

    /**
     * Tells whether or not this channel's network socket is connected.
     *判断通道的网络socket是否连接
     * @return  <tt>true</tt> if, and only if, this channel's network socket
     *          is {@link #isOpen open} and connected
     当且仅当通道的网络socket打开,或者连接,则返回true
     */
    public abstract boolean isConnected();

    /**
     * Tells whether or not a connection operation is in progress on this
     * channel.  
     *判断一个通道是不是正在进行连接操作。
     * @return  <tt>true</tt> if, and only if, a connection operation has been
     *          initiated on this channel but not yet completed by invoking the
     *          {@link #finishConnect finishConnect} method
     当且仅当一个连接操作正在进行,还没有完成,调用#finishConnect方法,完成连接。
     */
    public abstract boolean isConnectionPending();

    /**
     * Connects this channel's socket.
     *连接通道的socket
     *  If this channel is in non-blocking mode then an invocation of this
     * method initiates a non-blocking connection operation.  If the connection
     * is established immediately, as can happen with a local connection, then
     * this method returns <tt>true</tt>.  Otherwise this method returns
     * <tt>false</tt> and the connection operation must later be completed by
     * invoking the {@link #finishConnect finishConnect} method.
     *如果通道是非阻塞模式,调用此方法将会初始化一个非阻塞的连接操作。如果连接立即
     建立,则伴随着一个本地的连接,方法将会返回true。如果连接操作通过调用#finishConnect
     方法完成,则方法返回false。
     * <p> If this channel is in blocking mode then an invocation of this
     * method will block until the connection is established or an I/O error
     * occurs.
     *如果通道是阻塞模式的,方法将会阻塞到连接建立或者一个IO操作异常。
     * <p> This method performs exactly the same security checks as the {@link
     * java.net.Socket} class.  That is, if a security manager has been
     * installed then this method verifies that its {@link
     * java.lang.SecurityManager#checkConnect checkConnect} method permits
     * connecting to the address and port number of the given remote endpoint.
     *此方法将进行与java.net.Socket相同的安全检查。如果如果安全管理器配置,这个方法将会
     通过java.lang.SecurityManager#checkConnect检查是否允许连接到远程终端的地址和port。
     * <p> This method may be invoked at any time.  If a read or write
     * operation upon this channel is invoked while an invocation of this
     * method is in progress then that operation will first block until this
     * invocation is complete.  If a connection attempt is initiated but fails,
     * that is, if an invocation of this method throws a checked exception,
     * then the channel will be closed.  

     *此方法可以在任何时候调用。如果当前线程调用读写操作时,有其他线程正在进程
     读写操作,则当前线程阻塞到其他线程完成读写操作后,方可读写通道。如果连接已经初始化,
     单连接失败调用此方法将会抛出一个检查异常,然后关闭通道。
     * @param  remote
     *         The remote address to which this channel is to be connected
     *
     * @return  <tt>true</tt> if a connection was established,
     *          <tt>false</tt> if this channel is in non-blocking mode
     *          and the connection operation is in progress
     *
     * @throws  AlreadyConnectedException
     *          If this channel is already connected
     *
     * @throws  ConnectionPendingException
     *          If a non-blocking connection operation is already in progress
     *          on this channel
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the connect operation is in progress
     *
     * @throws  ClosedByInterruptException
     *          If another thread interrupts the current thread
     *          while the connect operation is in progress, thereby
     *          closing the channel and setting the current thread's
     *          interrupt status
     *
     * @throws  UnresolvedAddressException
     *          If the given remote address is not fully resolved
     *
     * @throws  UnsupportedAddressTypeException
     *          If the type of the given remote address is not supported
     *
     * @throws  SecurityException
     *          If a security manager has been installed
     *          and it does not permit access to the given remote endpoint
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public abstract boolean connect(SocketAddress remote) throws IOException;

    /**
     * Finishes the process of connecting a socket channel.
     *完成到socket通道的连接任务。
     *  A non-blocking connection operation is initiated by placing a socket
     * channel in non-blocking mode and then invoking its {@link #connect
     * connect} method.  Once the connection is established, or the attempt has
     * failed, the socket channel will become connectable and this method may
     * be invoked to complete the connection sequence.  If the connection
     * operation failed then invoking this method will cause an appropriate
     * {@link java.io.IOException} to be thrown.
     *在设置socket通道非阻塞模式时,一个非阻塞的连接操作初始化,然后调用#connect方法建立
     到通道连接。如果连接已经建立或尝试连接失败,在finishConnect方法调用完成连接工作后,
     socket通道将会变得connectable。如果一个连接操作失败,调用此方将会抛出IO异常。
     * <p> If this channel is already connected then this method will not block
     * and will immediately return <tt>true</tt>.  If this channel is in
     * non-blocking mode then this method will return <tt>false</tt> if the
     * connection process is not yet complete.  If this channel is in blocking
     * mode then this method will block until the connection either completes
     * or fails, and will always either return <tt>true</tt> or throw a checked
     * exception describing the failure.
     *如果通道已经连接,则此方法不会阻塞,将会立即返回true。如果通道是非阻塞模式,在连接还没有
     建立时,此方法将会返回false。如果通道是阻塞模式,调用finishConnect将会阻塞到连接操作完成
     或者失败,返回true或者抛出一个不检查的异常描述失败。
     * <p> This method may be invoked at any time.  If a read or write
     * operation upon this channel is invoked while an invocation of this
     * method is in progress then that operation will first block until this
     * invocation is complete.  If a connection attempt fails, that is, if an
     * invocation of this method throws a checked exception, then the channel
     * will be closed.  

     *此方法可以在任何时候调用。如果当前线程调用读写操作时,有其他线程正在进程
     读写操作,则当前线程阻塞到其他线程完成读写操作后,方可读写通道。如果连接已经初始化,
     单连接失败调用此方法将会抛出一个检查异常,然后关闭通道。
     * @return  <tt>true</tt> if, and only if, this channel's socket is now
     *          connected
     *
     * @throws  NoConnectionPendingException
     *          If this channel is not connected and a connection operation
     *          has not been initiated
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the connect operation is in progress
     *
     * @throws  ClosedByInterruptException
     *          If another thread interrupts the current thread
     *          while the connect operation is in progress, thereby
     *          closing the channel and setting the current thread's
     *          interrupt status
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public abstract boolean finishConnect() throws IOException;

    /**
     * Returns the remote address to which this channel's socket is connected.
     *返回通道socket连接的远端地址。
     * <p> Where the channel is bound and connected to an Internet Protocol
     * socket address then the return value from this method is of type {@link
     * java.net.InetSocketAddress}.
     *在通道绑定地址并连接到一个网络协议的socket地址,此方法返回的值为类型为java.net.InetSocketAddress
     * @return  The remote address; {@code null} if the channel's socket is not
     *          connected
     *
     * @throws  ClosedChannelException
     *          If the channel is closed
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since 1.7
     */
    public abstract SocketAddress getRemoteAddress() throws IOException;

    // -- ByteChannel operations --

    /**
    从通道读取字节序列,写到缓存区
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public abstract int read(ByteBuffer dst) throws IOException;

    /**
     从通道读取字节序列,写到一组缓存区
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public abstract long read(ByteBuffer[] dsts, int offset, int length)
        throws IOException;

    /**
     与read(ByteBuffer[] dsts, int offset, int length)含义相同
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public final long read(ByteBuffer[] dsts) throws IOException {
        return read(dsts, 0, dsts.length);
    }

    /**
    从缓冲区读取字节序列,写到通道
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public abstract int write(ByteBuffer src) throws IOException;

    /**
    从一组缓冲区读取字节序列,写到通道
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public abstract long write(ByteBuffer[] srcs, int offset, int length)
        throws IOException;

    /**
     与方法write(ByteBuffer[] srcs, int offset, int length)无异
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public final long write(ByteBuffer[] srcs) throws IOException {
        return write(srcs, 0, srcs.length);
    }
}

附:


  • 大小: 57 KB
0
1
分享到:
评论

相关推荐

    Thrift demo

    它旨在通过定义一种简单、高效的接口定义语言(IDL)来构建可伸缩的分布式系统。Thrift 允许开发者定义数据类型和服务接口,然后自动生成相应的代码,用于各种编程语言,如 Java、Python、C++、PHP 等,实现高效、...

    Socket实现聊天室功能

    Socket是网络通信中的一个抽象接口,可以看作是两台计算机之间的通信通道。在Java中,`java.net.Socket`类代表客户端Socket,用于连接到服务器;而`java.net.ServerSocket`类则用于创建服务器端监听套接字,等待...

    笔记,4、深入Netty1

    `NioEventLoop`执行了`EventLoop`接口,它不仅处理I/O操作,还负责调度和执行用户定义的任务。每个`NioEventLoop`都有自己的线程,确保了并发和并行性。 4. **NioEventLoopGroup**: 可以看作是一组`NioEventLoop`的...

    Java面试总结

    - Java 8扩展了注解的使用场景,允许在更多的上下文中使用注解,包括局部变量、接口、超类和异常定义等。 **8. java.time包** - Java 8新增了`java.time`包,用于处理日期、时间、时区等。这些类设计得更加现代,...

    Netty实现Java服务端和C#客户端联通

    在Netty中,可以通过ChannelHandlerContext发送和接收数据,而在DotNetty中,可以使用IChannel接口进行相应操作。 为了确保数据的正确传输和解析,开发者需要定义一套序列化和反序列化的机制,例如JSON、protobuf或...

    Mina实现RPC的例子

    1. **定义服务接口**:首先,我们需要在服务提供者端定义一个服务接口,该接口包含了所有可供远程调用的方法。例如: ```java public interface MyService { String doSomething(String input); } ``` 2. **...

    java学习路线.docx

    - **接口与抽象类:** 接口定义了一组行为规范,没有具体实现;抽象类则可以包含抽象方法(无实现的方法)和具体方法。学习如何定义接口和抽象类,并在子类中实现这些接口或抽象方法。 - **内部类:** 内部类是一...

    java 聊天代码 9

    类与接口定义 - **`ChatClient` 类**:这是一个实现了聊天功能的客户端类。 - **`MyClientListener` 类**:实现了 `ActionListener` 接口,用于处理用户界面中的按钮点击事件。 #### 2. 主方法 (`main` 方法) - ...

    java编程思想笔记

    此外,`java.nio`包的`SocketChannel`和`ServerSocketChannel`提供了更高效的数据传输方式。 以上是Java编程思想笔记中涵盖的主要知识点,这些内容不仅帮助初学者建立起对Java语言的全面认识,也是进阶开发者必备的...

    多线程NIO客户端实例

    - 定义了服务器的地址`host`和端口`port`,以及用于读写数据的`ByteBuffer`对象。 2. **线程启动和SocketChannel创建**: - `Kfd`构造函数中,调用了`start()`方法,启动了该线程。 - 使用`SocketChannel.open()...

    mina 实现简单通讯

    `IoHandler`接口定义了处理网络事件的方法,如连接建立、数据读取和断开连接。在实际应用中,我们需要实现这个接口来定义我们的业务逻辑。 描述中提到了“累积协议编解码器”,这是MINA中的关键概念。MINA提供了...

    JAVA NIO 异步通信客户端

    9. **CompletionHandler**: 在异步通信中,可以使用CompletionHandler接口定义一个回调函数,当I/O操作完成时,这个函数会被调用,处理结果。 10. **安全性与异常处理**: 考虑到网络通信的不可靠性,客户端需要适当...

    java NOI 学习

    1. **Channels**:`Channel`接口定义了与实体(如文件或网络连接)交互的基本方式。`Channel`有两种基本类型:`BlockingChannel`和`NonBlockingChannel`。`Channel`的操作主要涉及读取和写入`Buffer`。 - `...

    java笔试题大集合及答案(另附各大公司笔试题).rar

    - **接口**:接口的定义、实现和多接口继承。 - **抽象类**:抽象类的定义和使用,以及抽象方法。 5. **集合框架**: - **List**:ArrayList、LinkedList的特性及操作,了解迭代器的使用。 - **Set**:HashSet...

    java nio 网络编程指南

    - **SelectableChannel**:这是一个接口,定义了可以支持非阻塞I/O操作的Channel的行为。它包括`ServerSocketChannel`和`SocketChannel`等具体实现。SelectableChannel提供了一种机制,让Channel能够在阻塞和非阻塞...

    java精通+开发案例 经典经典总结

    4. 接口与实现:接口是完全抽象的,用于定义行为规范,而实现则具体实现了这些规范。理解接口与实现的关系对于设计大型系统至关重要。 三、Java集合框架 1. 集合接口:List、Set和Map是Java集合框架的核心接口,...

    JDK_API_1_6_zh_CN.zip_jdk1.6

    API是编程者与操作系统、硬件和其他软件进行交互的接口,它定义了类库、接口、方法等,使得开发者能够构建稳定且高效的Java应用程序。 在JDK1.6版本中,API包含了大量的核心类库,这些类库涵盖了Java语言的各个方面...

    java.nio API详解

    4. `java.nio.charset.spi`:这是一个服务提供者接口(SPI)包,定义了实现Charset接口的抽象类,允许第三方添加新的字符集支持。 5. `java.nio.channels.spi`:类似的,这个包提供了Channel和Selector的SPI,用于...

    brpc-java是baidurpc的java版本实现

    1. **服务端实现**:首先,开发者需要定义服务接口,并实现该接口,然后通过brpc的注解标记服务及方法。 2. **服务注册**:启动服务时,服务端会自动注册到服务中心,使其他节点能够找到并调用。 3. **客户端调用*...

Global site tag (gtag.js) - Google Analytics