SocketChannel (Java Platform SE 8 ) (original) (raw)

public static SocketChannel open()
throws IOException
Returns:
A new socket channel
Throws:
[IOException](../../../java/io/IOException.html "class in java.io") - If an I/O error occurs

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 open() method, invoking the 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
Returns:
A new, and connected, socket channel
Throws:
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the connect operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - 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
[UnresolvedAddressException](../../../java/nio/channels/UnresolvedAddressException.html "class in java.nio.channels") - If the given remote address is not fully resolved
[UnsupportedAddressTypeException](../../../java/nio/channels/UnsupportedAddressTypeException.html "class in java.nio.channels") - If the type of the given remote address is not supported
[SecurityException](../../../java/lang/SecurityException.html "class in java.lang") - If a security manager has been installed and it does not permit access to the given remote endpoint
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public final int validOps()
Specified by:
[validOps](../../../java/nio/channels/SelectableChannel.html#validOps--) in class [SelectableChannel](../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")
Returns:
The valid-operation set

public abstract SocketChannel bind(SocketAddress local)
throws IOException
Binds the channel's socket to a local address.
This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the channel is closed. If the local parameter has the value null then the socket will be bound to an address that is assigned automatically.
Specified by:
[bind](../../../java/nio/channels/NetworkChannel.html#bind-java.net.SocketAddress-) in interface [NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels")
Parameters:
local - The address to bind the socket, or null to bind the socket to an automatically assigned socket address
Returns:
This channel
Throws:
[ConnectionPendingException](../../../java/nio/channels/ConnectionPendingException.html "class in java.nio.channels") - If a non-blocking connect operation is already in progress on this channel
[AlreadyBoundException](../../../java/nio/channels/AlreadyBoundException.html "class in java.nio.channels") - If the socket is already bound
[UnsupportedAddressTypeException](../../../java/nio/channels/UnsupportedAddressTypeException.html "class in java.nio.channels") - If the type of the given address is not supported
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If the channel is closed
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs
[SecurityException](../../../java/lang/SecurityException.html "class in java.lang") - If a security manager has been installed and itscheckListen method denies the operation
Since:
1.7
See Also:
NetworkChannel.getLocalAddress()

public abstract  SocketChannel setOption(SocketOption name,
T value)
throws IOException
Sets the value of a socket option.
Specified by:
[setOption](../../../java/nio/channels/NetworkChannel.html#setOption-java.net.SocketOption-T-) in interface [NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels")
Type Parameters:
T - The type of the socket option value
Parameters:
name - The socket option
value - The value of the socket option. A value of null may be a valid value for some socket options.
Returns:
This channel
Throws:
[UnsupportedOperationException](../../../java/lang/UnsupportedOperationException.html "class in java.lang") - If the socket option is not supported by this channel
[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang") - If the value is not a valid value for this socket option
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[IOException](../../../java/io/IOException.html "class in java.io") - If an I/O error occurs
Since:
1.7
See Also:
StandardSocketOptions

public abstract SocketChannel shutdownInput()
throws IOException
Shutdown the connection for reading without closing the channel.
Once shutdown for reading then further reads on the channel will return -1, the end-of-stream indication. If the input side of the connection is already shutdown then invoking this method has no effect.
Returns:
The channel
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs
Since:
1.7

public abstract SocketChannel shutdownOutput()
throws IOException
Shutdown the connection for writing without closing the channel.
Once shutdown for writing then further attempts to write to the channel will throw ClosedChannelException. If the output side of the connection is already shutdown then invoking this method has no effect.
Returns:
The channel
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs
Since:
1.7

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 Socket class.
Returns:
A socket associated with this channel

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 open and connected

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 thefinishConnect method

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 returnsfalse and the connection operation must later be completed by invoking the 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 Socket class. That is, if a security manager has been installed then this method verifies that its 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:
[AlreadyConnectedException](../../../java/nio/channels/AlreadyConnectedException.html "class in java.nio.channels") - If this channel is already connected
[ConnectionPendingException](../../../java/nio/channels/ConnectionPendingException.html "class in java.nio.channels") - If a non-blocking connection operation is already in progress on this channel
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the connect operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - 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
[UnresolvedAddressException](../../../java/nio/channels/UnresolvedAddressException.html "class in java.nio.channels") - If the given remote address is not fully resolved
[UnsupportedAddressTypeException](../../../java/nio/channels/UnsupportedAddressTypeException.html "class in java.nio.channels") - If the type of the given remote address is not supported
[SecurityException](../../../java/lang/SecurityException.html "class in java.lang") - If a security manager has been installed and it does not permit access to the given remote endpoint
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

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 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 appropriateIOException 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:
[NoConnectionPendingException](../../../java/nio/channels/NoConnectionPendingException.html "class in java.nio.channels") - If this channel is not connected and a connection operation has not been initiated
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the connect operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - 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
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public abstract SocketAddress getRemoteAddress()
throws IOException
Returns the remote address to which this channel's socket is connected.
Where the channel is bound and connected to an Internet Protocol socket address then the return value from this method is of type InetSocketAddress.
Returns:
The remote address; null if the channel's socket is not connected
Throws:
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If the channel is closed
[IOException](../../../java/io/IOException.html "class in java.io") - If an I/O error occurs
Since:
1.7

public abstract int read(ByteBuffer dst)
throws IOException
Reads a sequence of bytes from this channel into the given buffer.
An attempt is made to read up to r bytes from the channel, where r is the number of bytes remaining in the buffer, that is,dst.remaining(), at the moment this method is invoked.
Suppose that a byte sequence of length n is read, where0 <= n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index_p_ + n - 1, where p is the buffer's position at the moment this method is invoked. Upon return the buffer's position will be equal to_p_ + n; its limit will not have changed.
A read operation might not fill the buffer, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the channel. A socket channel in non-blocking mode, for example, cannot read any more bytes than are immediately available from the socket's input buffer; similarly, a file channel cannot read any more bytes than remain in the file. It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read.
This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.
Specified by:
[read](../../../java/nio/channels/ReadableByteChannel.html#read-java.nio.ByteBuffer-) in interface [ReadableByteChannel](../../../java/nio/channels/ReadableByteChannel.html "interface in java.nio.channels")
Parameters:
dst - The buffer into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the read operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public abstract long read(ByteBuffer[] dsts,
int offset,
int length)
throws IOException
Reads a sequence of bytes from this channel into a subsequence of the given buffers.
An invocation of this method attempts to read up to r bytes from this channel, where r is the total number of bytes remaining the specified subsequence of the given buffer array, that is,

dsts[offset].remaining()
+ dsts[offset+1].remaining()
+ ... + dsts[offset+length-1].remaining()
at the moment that this method is invoked.
Suppose that a byte sequence of length n is read, where0 <= n <= r. Up to the first dsts[offset].remaining() bytes of this sequence are transferred into buffer dsts[offset], up to the nextdsts[offset+1].remaining() bytes are transferred into bufferdsts[offset+1], and so forth, until the entire byte sequence is transferred into the given buffers. As many bytes as possible are transferred into each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit.
This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.
Specified by:
[read](../../../java/nio/channels/ScatteringByteChannel.html#read-java.nio.ByteBuffer:A-int-int-) in interface [ScatteringByteChannel](../../../java/nio/channels/ScatteringByteChannel.html "interface in java.nio.channels")
Parameters:
dsts - The buffers into which bytes are to be transferred
offset - The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger thandsts.length - offset
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the read operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public final long read(ByteBuffer[] dsts)
throws IOException
Reads a sequence of bytes from this channel into the given buffers.
An invocation of this method of the form c.read(dsts) behaves in exactly the same manner as the invocation

c.read(dsts, 0, dsts.length);
Specified by:
[read](../../../java/nio/channels/ScatteringByteChannel.html#read-java.nio.ByteBuffer:A-) in interface [ScatteringByteChannel](../../../java/nio/channels/ScatteringByteChannel.html "interface in java.nio.channels")
Parameters:
dsts - The buffers into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the read operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public abstract int write(ByteBuffer src)
throws IOException
Writes a sequence of bytes to this channel from the given buffer.
An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is,src.remaining(), at the moment this method is invoked.
Suppose that a byte sequence of length n is written, where0 <= n <= r. This byte sequence will be transferred from the buffer starting at index_p_, where p is the buffer's position at the moment this method is invoked; the index of the last byte written will be_p_ + n - 1. Upon return the buffer's position will be equal to_p_ + n; its limit will not have changed.
Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.
This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.
Specified by:
[write](../../../java/nio/channels/WritableByteChannel.html#write-java.nio.ByteBuffer-) in interface [WritableByteChannel](../../../java/nio/channels/WritableByteChannel.html "interface in java.nio.channels")
Parameters:
src - The buffer from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the write operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public abstract long write(ByteBuffer[] srcs,
int offset,
int length)
throws IOException
Writes a sequence of bytes to this channel from a subsequence of the given buffers.
An attempt is made to write up to r bytes to this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

srcs[offset].remaining()
+ srcs[offset+1].remaining()
+ ... + srcs[offset+length-1].remaining()
at the moment that this method is invoked.
Suppose that a byte sequence of length n is written, where0 <= n <= r. Up to the first srcs[offset].remaining() bytes of this sequence are written from buffer srcs[offset], up to the nextsrcs[offset+1].remaining() bytes are written from buffersrcs[offset+1], and so forth, until the entire byte sequence is written. As many bytes as possible are written from each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit.
Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.
This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.
Specified by:
[write](../../../java/nio/channels/GatheringByteChannel.html#write-java.nio.ByteBuffer:A-int-int-) in interface [GatheringByteChannel](../../../java/nio/channels/GatheringByteChannel.html "interface in java.nio.channels")
Parameters:
srcs - The buffers from which bytes are to be retrieved
offset - The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger thansrcs.length - offset
Returns:
The number of bytes written, possibly zero
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the write operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public final long write(ByteBuffer[] srcs)
throws IOException
Writes a sequence of bytes to this channel from the given buffers.
An invocation of this method of the form c.write(srcs) behaves in exactly the same manner as the invocation

c.write(srcs, 0, srcs.length);
Specified by:
[write](../../../java/nio/channels/GatheringByteChannel.html#write-java.nio.ByteBuffer:A-) in interface [GatheringByteChannel](../../../java/nio/channels/GatheringByteChannel.html "interface in java.nio.channels")
Parameters:
srcs - The buffers from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
[NotYetConnectedException](../../../java/nio/channels/NotYetConnectedException.html "class in java.nio.channels") - If this channel is not yet connected
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If this channel is closed
[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels") - If another thread closes this channel while the write operation is in progress
[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels") - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
[IOException](../../../java/io/IOException.html "class in java.io") - If some other I/O error occurs

public abstract SocketAddress getLocalAddress()
throws IOException
Returns the socket address that this channel's socket is bound to.
Where the channel is bound to an Internet Protocol socket address then the return value from this method is of type InetSocketAddress.
If there is a security manager set, its checkConnect method is called with the local address and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, a SocketAddress representing theloopback address and the local port of the channel's socket is returned.
Specified by:
[getLocalAddress](../../../java/nio/channels/NetworkChannel.html#getLocalAddress--) in interface [NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels")
Returns:
The SocketAddress that the socket is bound to, or theSocketAddress representing the loopback address if denied by the security manager, or null if the channel's socket is not bound
Throws:
[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") - If the channel is closed
[IOException](../../../java/io/IOException.html "class in java.io") - If an I/O error occurs