public class TCPSocket extends Socket
TCP Socket for connexions to servers.
This implementation creates a TCP Socket for client only connections.
To use the Socket you must connect to a remote endpoint using one of
the available connect or connectAsync methods available.
It is not recommended to call any bind methods because it will
replace the current socket (if it is connected) with a new not connected.
Receive operations will return when there is some data, that is, it will not
ensure read an exact quantity of bytes. Receive operations stores all unread
data into an intermediary Buffer. When it is full, new data gets discarded.
Internal buffer, normally, has capacity for 146KB (not KiB). To know if there's bytes
to read, you can call readableBytes().
Send operations will send directly to the remote endpoint, flushing anything pending to send. This also depends on if Nagle's Algorithm is enabled or not.
A TCP Connection can shutdown the output or the input or both streams. This can also be
accomplished with shutdownOutput(), shutdownOutputAsync(),
shutdownInput(), shutdownInputAsync(), shutdown() or
shutdownAsync().
When closed, all write and read operations will fail. Socket.isOpen() will return null.
Is possible to be notified when the socket is closed, by you or by the remote endpoint,
using onClose(), or the synchronous version waitUntilClose().
| Constructor and Description |
|---|
TCPSocket(IOService service)
Creates a new TCP Socket
|
| Modifier and Type | Method and Description |
|---|---|
void |
bind(java.net.SocketAddress address)
Binds the socket to a local address.
|
void |
close()
Closes the socket after the current operation is done.
|
Future<java.lang.Void> |
closeAsync()
Closes the socket after the current operation is done, and then
calls
cbk with the result, either successful or failure. |
void |
connect(java.net.SocketAddress address)
Binds the socket to a random port and connects to the remote
endpoint.
|
Future<java.lang.Void> |
connectAsync(java.net.SocketAddress endpoint)
Binds the socket to a random port and connects to the remote endpoint
asynchronously.
|
Future<java.lang.Void> |
onClose()
Returns a
Future that will complete when this socket is
closed. |
int |
readableBytes() |
long |
receive(io.netty.buffer.ByteBuf data,
int bytes)
Receives some data from the socket and writes it into the
ByteBuf
data a maximum of bytes bytes. |
Future<java.lang.Long> |
receiveAsync(io.netty.buffer.ByteBuf data,
int bytes)
Receives some data from the socket and writes it into the
ByteBuf
data a maximum of bytes bytes. |
void |
shutdown()
Ends the input and output stream of this connection, but not closes
the connection.
|
Future<java.lang.Void> |
shutdownAsync()
Ends the input and output stream of this connection, but not closes
the connection.
|
void |
shutdownInput()
Ends the input stream of this connection, discarding any pending data to be
acknowledged.
|
Future<java.lang.Void> |
shutdownInputAsync()
Ends the input stream of this connection, discarding any pending data to be
acknowledged.
|
void |
shutdownOutput()
Ends the output stream of this connection, sending before any data pending
to send.
|
Future<java.lang.Void> |
shutdownOutputAsync()
Ends the output stream of this connection, sending before any data pending
to send.
|
void |
waitUntilClose()
Waits until this socket is closed.
|
addOnDataReceivedListener, bind, checkSocketCreated, connect, connect, connectAsync, connectAsync, createFuture, createFuture, fireReceivedData, isOpen, localEndpoint, receive, receiveAsync, receivedBytes, remoteEndpoint, send, send, send, sendAsync, sendAsync, sendAsync, sendBytes, setOptionpublic void bind(java.net.SocketAddress address)
Socketpublic void connect(java.net.SocketAddress address)
throws java.lang.InterruptedException
Socketpublic Future<java.lang.Void> connectAsync(java.net.SocketAddress endpoint)
SocketFuture where the task can be managed.connectAsync in class Socketendpoint - remote endpoint to connectFuture of the taskpublic long receive(io.netty.buffer.ByteBuf data,
int bytes)
throws java.lang.Throwable
SocketByteBuf
data a maximum of bytes bytes. This method don't read exactly
bytes bytes, only at most. To ensure read all the bytes, use one of the
SocketUtil methods.public Future<java.lang.Long> receiveAsync(io.netty.buffer.ByteBuf data, int bytes)
SocketByteBuf
data a maximum of bytes bytes. This method don't read exactly
bytes bytes, only at most. To ensure read all the bytes, use one of the
SocketUtil methods. This operation is done asynchronously, so returns a
Future for the task.receiveAsync in class Socketdata - buffer where to write on all the databytes - maximum number of bytes to readFuture representing this taskpublic void shutdownOutput()
IOException.public Future<java.lang.Void> shutdownOutputAsync()
IOException.Future of this taskpublic void shutdownInput()
IOException
or returning -1.
IOException.public Future<java.lang.Void> shutdownInputAsync()
IOException
or returning -1.
IOException.Future of this taskpublic void shutdown()
public Future<java.lang.Void> shutdownAsync()
Future of this taskAbout shutdown the input stream,
About shutdown the Output streampublic int readableBytes()
public void waitUntilClose()
public Future<java.lang.Void> onClose()
Future that will complete when this socket is
closed.Future for the taskpublic void close()
SocketThrowable if an error occurs. This operation
blocks the thread.public Future<java.lang.Void> closeAsync()
Socketcbk with the result, either successful or failure.closeAsync in class Socket