This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.nio.channels
class SocketChannel

java.lang.Object extended by java.nio.channels.spi.AbstractInterruptibleChannel extended by java.nio.channels.SelectableChannel extended by java.nio.channels.spi.AbstractSelectableChannel extended by java.nio.channels.SocketChannel
All Implemented Interfaces:
ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ScatteringByteChannel

Most common ways to construct:

ServerSocketChannel server = …;

SocketChannel client = server.accept();

Based on 57 examples

 

SelectionKey key = …;

SocketChannel sc = (SocketChannel)key.channel();

Based on 55 examples


public abstract class SocketChannel
extends AbstractSelectableChannel
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel

A selectable channel for stream-oriented connecting sockets.

Socket channels are not a complete abstraction of connecting network sockets. Binding, shutdown, and the manipulation of socket options must be done through an associated {@link java.net.Socket} object obtained by invoking the {@link #socket() socket} method. It is not possible to create a channel for an arbitrary, pre-existing socket, nor is it possible to specify the {@link java.net.SocketImpl} object to be used by a socket associated with a socket channel.

A socket channel is created by invoking one of the {@link #open open} methods of this class. 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 channels support non-blocking connection: 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.

The input and output sides of a socket channel may independently be shut down without actually closing the channel. Shutting down the input side of a channel by invoking the {@link java.net.Socket#shutdownInput shutdownInput} method of an associated socket object will cause further reads on the channel to return -1, the end-of-stream indication. Shutting down the output side of the channel by invoking the {@link java.net.Socket#shutdownOutput shutdownOutput} method of an associated socket object will cause further writes on the channel to throw a {@link ClosedChannelException}.

Socket channels support asynchronous shutdown, 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 -1. 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 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.


Constructor Summary
protected

          Initializes a new instance of this class.
 
Method Summary
abstract boolean

          Connects this channel's socket.
abstract boolean

          Finishes the process of connecting a socket channel.
abstract boolean

          Tells whether or not this channel's network socket is connected.
abstract boolean

          Tells whether or not a connection operation is in progress on this channel.
static SocketChannel

          Opens a socket channel.
static SocketChannel

          Opens a socket channel and connects it to a remote address.
abstract int

          
 long
read(ByteBuffer[] dsts)

          
abstract long
read(ByteBuffer[] dsts, int offset, int length)

          
abstract Socket

          Retrieves a socket associated with this channel.
 int

          Returns an operation set identifying this channel's supported operations.
abstract int

          
 long
write(ByteBuffer[] srcs)

          
abstract long
write(ByteBuffer[] srcs, int offset, int length)

          
 
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel
blockingLock, configureBlocking, implCloseChannel, implCloseSelectableChannel, implConfigureBlocking, isBlocking, isRegistered, keyFor, provider, register
 
Methods inherited from class java.nio.channels.SelectableChannel
blockingLock, configureBlocking, isBlocking, isRegistered, keyFor, provider, register, register, validOps
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, implCloseChannel, isOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketChannel

protected SocketChannel(SelectorProvider provider)
Initializes a new instance of this class.

Parameters:
provider
Method Detail

connect

public abstract boolean connect(SocketAddress remote)
                         throws IOException
Connects this channel's 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 true. Otherwise this method returns false and the connection operation must later be completed by invoking the {@link #finishConnect finishConnect} method.

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.

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.

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.

Parameters:
remote - The remote address to which this channel is to be connected
Returns:
true if a connection was established, false if this channel is in non-blocking mode and the connection operation is in progress
Throws:
IOException - If some other I/O error occurs

finishConnect

public abstract boolean finishConnect()
                               throws IOException
Finishes the process of connecting a socket channel.

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.

If this channel is already connected then this method will not block and will immediately return true. If this channel is in non-blocking mode then this method will return false 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 true or throw a checked exception describing the failure.

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.

Returns:
true if, and only if, this channel's socket is now connected
Throws:
IOException - If some other I/O error occurs

isConnected

public abstract boolean isConnected()
Tells whether or not this channel's network socket is connected.

Returns:
true if, and only if, this channel's network socket is connected

isConnectionPending

public abstract boolean isConnectionPending()
Tells whether or not a connection operation is in progress on this channel.

Returns:
true if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the {@link #finishConnect finishConnect} method

open

public static SocketChannel open()
                          throws IOException
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.

Returns:
A new socket channel
Throws:
IOException - If an I/O error occurs

open

public static SocketChannel open(SocketAddress remote)
                          throws IOException
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 remote, and then returning that channel.

Parameters:
remote - The remote address to which the new channel is to be connected
Throws:
IOException - If some other I/O error occurs

read

public abstract int read(ByteBuffer dst)
                  throws IOException
Parameters:
dst
Throws:
IOException

read

public final long read(ByteBuffer[] dsts)
                throws IOException
Parameters:
dsts
Throws:
IOException

read

public abstract long read(ByteBuffer[] dsts,
                          int offset,
                          int length)
                   throws IOException
Parameters:
dsts
offset
length
Throws:
IOException

socket

public abstract Socket socket()
Retrieves a socket associated with this channel.

The returned object will not declare any public methods that are not declared in the {@link java.net.Socket} class.

Returns:
A socket associated with this channel

validOps

public final int validOps()
Returns an operation set identifying this channel's supported operations.

Socket channels support connecting, reading, and writing, so this method returns ({@link SelectionKey#OP_CONNECT} | {@link SelectionKey#OP_READ} | {@link SelectionKey#OP_WRITE}).

Overrides:
validOps in class SelectableChannel
Returns:
The valid-operation set

write

public abstract int write(ByteBuffer src)
                   throws IOException
Parameters:
src
Throws:
IOException

write

public final long write(ByteBuffer[] srcs)
                 throws IOException
Parameters:
srcs
Throws:
IOException

write

public abstract long write(ByteBuffer[] srcs,
                           int offset,
                           int length)
                    throws IOException
Parameters:
srcs
offset
length
Throws:
IOException


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.