Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 457
نمايش فراداده

SocketChanneljava.nio.channels

Java 1.4closeable

This class is a channel for communicating over a java.net.Socket. It implements ReadableByteChannel and WriteableByteChannel as well as GatheringByteChannel and ScatteringByteChannel. It is a subclass of SelectableChannel and can be used with a Selector.

Create a new SocketChannel with one of the static open( ) methods. The no-argument version of open( ) creates a new SocketChannel but does not connect it to a remote host. The other version of open( ) opens a new channel and connects it to the specified java.net.SocketAddress. If you create an unconnected socket, you can explictly connect it with the connect( ) method. The main reason to open the channel and connect to the remote host in separate steps is if you want to do a nonblocking connect. To do this, first put the channel into nonblocking mode with the inherited configureBlocking( ) method. Then, call connect( ): it will return immediately, without waiting for the connection to be established. Then register the channel with a Selector specifying that you are interested in SelectionKey.OP_CONNECT operations. When you are notified that your channel is ready to connect (see Selector and SelectionKey for details) simply call the nonblocking finishConnect( ) method to complete the connection. isConnected( ) returns TRue once a connection is established, and false otherwise. isConnectionPending( ) returns true if connect( ) has been called in blocking mode and has not yet returned, or if connect( ) has been called in nonblocking mode, but finishConnect( ) has not been called yet.

Once you have opened and connected a SocketChannel, you can read and write bytes to it with the various read( ) and write( ) methods. SocketChannel is thread-safe: read and write operations may proceed concurrently, but SocketChannel will not allow more than one read operation and more than one write operation to proceed at the same time. If you place a SocketChannel into nonblocking mode, you can register it with a Selector using the SelectionKey constants OP_READ and OP_WRITE, to have the Selector tell you when the channel is ready for reading or writing.

The socket( ) method returns the java.net.Socket that is associated with the SocketChannel. You can use this Socket object to configure socket options, bind the socket to a specific local address, close the socket, or shutdown its input or output sides. See java.net.Socket. Note that although all SocketChannel objects have associated Socket objects, the reverse is not true: you cannot obtain a SocketChannel from a Socket unless the Socket was created along with the SocketChannel by a call to SocketChannel.open( ).

When you are done with a SocketChannel, close it with the close( ) method. You can also independently shut down the read and write portions of the channel with socket( ).shutdownInput( ) and socket( ).shutdownOutput( ). When the input is shut down, any future reads (and any blocked read operation) will return -1 to indicate that the end-of-stream has been reached. When the output is shut down, any future writes throw a ClosedChannelException, and any write operation that was blocked at the time of shut down throws a AsynchronousCloseException.

Figure 13-39. java.nio.channels.SocketChannel

public abstract class

SocketChannel extends java.nio.channels.spi. AbstractSelectableChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel { // Protected Constructors protected

SocketChannel (java.nio.channels.spi.SelectorProvider

provider ); // Public Class Methods public static SocketChannel

open ( ) throws java.io.IOException; public static SocketChannel

open (java.net.SocketAddress

remote ) throws java.io.IOException; // Public Instance Methods public abstract boolean

connect (java.net.SocketAddress

remote ) throws java.io.IOException; public abstract boolean

finishConnect ( ) throws java.io.IOException; public abstract boolean

isConnected ( ); public abstract boolean

isConnectionPending ( ); public abstract java.net.Socket

socket ( ); // Methods Implementing GatheringByteChannel public final long

write (java.nio.ByteBuffer[ ]

srcs ) throws java.io.IOException; public abstract long

write (java.nio.ByteBuffer[ ]

srcs , int

offset , int

length ) throws java.io.IOException; // Methods Implementing ReadableByteChannel public abstract int

read (java.nio.ByteBuffer

dst ) throws java.io.IOException; // Methods Implementing ScatteringByteChannel public final long

read (java.nio.ByteBuffer[ ]

dsts ) throws java.io.IOException; public abstract long

read (java.nio.ByteBuffer[ ]

dsts , int

offset , int

length ) throws java.io.IOException; // Methods Implementing WritableByteChannel public abstract int

write (java.nio.ByteBuffer

src ) throws java.io.IOException; // Public Methods Overriding SelectableChannel public final int

validOps ( ); }

Returned By

java.net.Socket.getChannel( ), ServerSocketChannel.accept( ), java.nio.channels.spi.SelectorProvider.openSocketChannel( )