Java NIO Socket Channel

Source: Internet
Author: User
Tags set socket

In Java NIO, the channel is used to abstract the connection between the program and the principal I/O operations, including files, sockets, or other devices. In short, it refers to a connection between an IO operator and an object.

According to the channel interface definition, the channel only open and closed two states, only when the channel is in the open state for its operation, and the operation of the closed channel will cause an exception thrown. The corresponding channel interface also has only isopen () and Close () methods.

In socket programming, the commonly used channel classes are Serversocketchannel and Socketchannel. The Serversocketchannel represents a socket connection that is in the listening state as the socket server. The Socketchannel represents an already established socket connection.

Serversocketchannel has the Accept () method, and when the method returns successfully, it returns an already established Socketchannle object, which also represents the connection and difference between the two socket channel types that represent different states. Serversocketchannel is bound to the local address through the bind () method, and can be set through the SetOption () method SO_RCVBUF (socket receive buffer size) and SO_REUSEADDR (whether to reuse the socket address, Since the socket needs to go through multiple states during shutdown, this option is typically useful in scenarios where a fast restart is required) two socket options.

The Socketchannel uses the Connet () method to establish a connection, and supports non-blocking builds, which are combined by isconnetionpending () and Finishconnect () to determine the final state of the connection in a non-blocking state. Socketchannel also supports asynchronous shutdown, if the write thread actively closes the channel, the concurrent read operation will not read the content, and if the read thread actively closes the channel, the concurrent write operation will be asynchronouscloseexception. The same pair of Socketchannle can be set by the SetOption () method for several options, including: So_sndbuf (send buffer), SO_RCVBUF, so_keepalive (whether the keepalive mechanism implemented by the protocol stack, default to False), SO_REUSEADDR, So_linger (shut-down wait time under a blocking connection), Tcp_nodelay (whether Nagle algorithm is disabled).

The related class diagrams for these two classes are as follows:

As can be seen, both Serversocketchannel and Socketchannel inherit Selectablechannel to enable non-blocking IO with selector mates. Socketchannel inherits several channel supports for read and write operations to support read and write operations.

In addition, the channel read and write needs to use buffer, visible in the NIO buffer is the program of other objects and channel interaction intermediary, through the buffer and channel to achieve the I/O object read and write operations.

Several key interfaces are defined as follows

  

Public interface Channel {/** * Determines whether to turn on * @return */public boolean isOpen ();p ublic void Close () throws IOException;} /** * can interrupt channel</p> * 1. Can be closed asynchronously. If the current thread is blocking the read and write operation of the channel,</l> * The current thread receives a asynchronouscloseexception</p> * 2 when another thread calls the close operation. can be interrupted. If the current thread is blocking the channel, </l> * causes the channel Close when other threads trigger the interrupt method, and the current thread receives a closedbyinterruptexception, * Its interrupt status is set </p> * * 3. If the current thread interrupt state has been set, the,</l> * channel will be closed when the current channel's blocking IO operation is called, and the thread will receive closedbyinterruptexception. </l> * Its interrupt status remains constant .</p> * * @author Luojiahu * */public interface Interruptiblechannel extends Channel{p ublic void Close () throws IOException;} /** * Readable byte channel * can be read for current Channel blocking. If there are other lines is impersonating read operations on the current channel,</l> * Call read will block until the other last read operation is complete. * * @author Luojiahu * */public interface Readablebytechannel extends Channel {/** * read content from current Channel write buffer</l> * The ability to read content depends on the current Channel's state .</l> * for a non-blocking socket Channel, if the current socket's </l> * Receive buffer does not have any content, then nothing can be written to SRC *@param src * @return * @throws ioexception */public int read (Bytebuffer src) throws IOException;} /** * Writable Byte Channel * can block the current Channel write .</l> * @author Luojiahu * */public interface Writablebytechannel extends Chann El {/** * reads content from DST buffer write to current channel</l> * whether the content can be written depends on the current Channel status </l> * For non-blocking Socket channel, only write less than </ L> * Corresponds to write buffer writable size content * * @param DST * @throws ioexception */public void Write (Bytebuffer dst) throws IOException;} /** * Network Socket channel</l> * @author Luojiahu * */public interface Networkchannel extends channel{/** * bind to local address </ L> * Once bound, bind until the channel is closed </l> * If the parameter is not empty, bind to the Automatically assigned address * @param local * @return * @throws IOException */networkchannel bind  (SocketAddress local) throws ioexception;/** * Get the bound address * @return * @throws ioexception */socketaddress getlocaladdress () Throws ioexception;/** * Set socket option * @param name * @param value * @return * @throws ioexception */<t> Networkchan Nel setOption (socketoption<t> name, T value) throws IOException;} 

  

Java NIO Socket Channel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.