public class UDPSocket extends Socket
UDP socket for any kind of operation with them.
This implementation creates a UDP socket that can act as
server or client, or both. To use this socket you can either
Socket.bind() to a random port, bind(SocketAddress) to
a known port or connect to a remote endpoint. In UDP,
connect is not defined, instead we use this connection
to discard any message that doesn't come from the remote endpoint
and send only to the remote endpoint. A not connected socket could
send to any endpoint and receive from any endpoint, using the special
receiveFrom and sendTo methods.
Receive operations reads message by message, that's the essence of
User Datagram Protocol (UDP). Any received packet is stored in a queue
and every receive operation will read only one of this, if
is possible. If the buffer not has enough capacity, will fail
the receive operation. In this case, will throw a UDPSocket.NotEnoughSpaceForPacketException
with some useful information about the packet. In case of an asynchronous
operation, you must read the packet inside the callback using a
synchronous receive operation, otherwise, the packet will be
discarded.
Send operations also goes message by message. Every message is sent directly, without any intermediary buffers nor queue (except by the OS).
Received messages from any sender (receiveFrom operations) will return
a UDPSocket.Packet which stores information about the Datagram received. The
buffer inside it is your buffer.
| Modifier and Type | Class and Description |
|---|---|
static class |
UDPSocket.NotEnoughSpaceForPacketException
When there isn't enough space to save a Datagram, this exception is raised to notify
the programmer to pass a more bigger buffer.
|
class |
UDPSocket.Packet
Represents a Datagram of UDP
|
| Constructor and Description |
|---|
UDPSocket(IOService service)
Create a UDP Socket
|
UDPSocket(IOService service,
java.net.ProtocolFamily ip)
Create a UDP Socket
|
| Modifier and Type | Method and Description |
|---|---|
void |
bind(java.net.SocketAddress local)
Binds the socket to a local address.
|
void |
connect(java.net.SocketAddress endpoint)
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.
|
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. |
void |
receive(Serializable data) |
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. |
<Type extends Serializable> |
receiveAsync(Type data) |
Future<UDPSocket.Packet> |
receiveAsyncFrom(io.netty.buffer.ByteBuf data) |
Future<UDPSocket.Packet> |
receiveAsyncFrom(io.netty.buffer.ByteBuf data,
int bytes) |
<Type extends Serializable> |
receiveAsyncFrom(Type data) |
UDPSocket.Packet |
receiveFrom(io.netty.buffer.ByteBuf data) |
UDPSocket.Packet |
receiveFrom(io.netty.buffer.ByteBuf data,
int bytes) |
UDPSocket.Packet |
receiveFrom(Serializable data) |
long |
send(Serializable data) |
Future<java.lang.Void> |
sendAsync(Serializable data) |
Future<java.lang.Void> |
sendAsyncTo(io.netty.buffer.ByteBuf data,
java.net.InetSocketAddress endpoint) |
Future<java.lang.Void> |
sendAsyncTo(io.netty.buffer.ByteBuf data,
int bytes,
java.net.InetSocketAddress endpoint) |
Future<java.lang.Void> |
sendAsyncTo(Serializable data,
java.net.InetSocketAddress remoteEndpoint) |
Future<java.lang.Void> |
sendAsyncTo(java.lang.String data,
java.net.InetSocketAddress remoteEndpoint) |
long |
sendTo(io.netty.buffer.ByteBuf data,
java.net.InetSocketAddress endpoint)
Sends the data contained in
data with a length of
data.remaining() to the remote endpoint endpoint. |
long |
sendTo(io.netty.buffer.ByteBuf data,
int bytes,
java.net.InetSocketAddress endpoint)
Sends the data contained in
data with a length of bytes
to the remote endpoint endpoint. |
long |
sendTo(Serializable data,
java.net.InetSocketAddress remoteEndpoint) |
long |
sendTo(java.lang.String data,
java.net.InetSocketAddress remoteEndpoint) |
addOnDataReceivedListener, bind, checkSocketCreated, close, closeAsync, connect, connect, connectAsync, connectAsync, createFuture, createFuture, fireReceivedData, isOpen, localEndpoint, receive, receiveAsync, receivedBytes, remoteEndpoint, send, send, send, sendAsync, sendAsync, sendAsync, sendBytes, setOptionpublic UDPSocket(IOService service)
service - IOService to attach this socketpublic void bind(@NotNull
java.net.SocketAddress local)
Socketpublic void connect(@NotNull
java.net.SocketAddress endpoint)
throws java.lang.InterruptedException
Socket@NotNull public Future<java.lang.Void> connectAsync(@NotNull java.net.SocketAddress endpoint)
SocketFuture where the task can be managed.connectAsync in class Socketendpoint - remote endpoint to connectFuture of the taskpublic long sendTo(io.netty.buffer.ByteBuf data,
int bytes,
java.net.InetSocketAddress endpoint)
data with a length of bytes
to the remote endpoint endpoint.data - data to sendbytes - number of bytes to sendendpoint - remote endpointpublic long sendTo(io.netty.buffer.ByteBuf data,
java.net.InetSocketAddress endpoint)
data with a length of
data.remaining() to the remote endpoint endpoint.data - data to sendendpoint - remote endpointpublic Future<java.lang.Void> sendAsyncTo(io.netty.buffer.ByteBuf data, int bytes, java.net.InetSocketAddress endpoint)
public Future<java.lang.Void> sendAsyncTo(io.netty.buffer.ByteBuf data, java.net.InetSocketAddress endpoint)
public 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 UDPSocket.Packet receiveFrom(io.netty.buffer.ByteBuf data, int bytes) throws java.lang.Throwable
java.lang.Throwablepublic UDPSocket.Packet receiveFrom(io.netty.buffer.ByteBuf data) throws java.lang.Throwable
java.lang.Throwable@NotNull 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 Future<UDPSocket.Packet> receiveAsyncFrom(io.netty.buffer.ByteBuf data, int bytes)
public Future<UDPSocket.Packet> receiveAsyncFrom(io.netty.buffer.ByteBuf data)
public Future<java.lang.Void> sendAsync(Serializable data)
public long send(Serializable data) throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic Future<java.lang.Void> sendAsyncTo(java.lang.String data, java.net.InetSocketAddress remoteEndpoint)
public Future<java.lang.Void> sendAsyncTo(Serializable data, java.net.InetSocketAddress remoteEndpoint)
public long sendTo(java.lang.String data,
java.net.InetSocketAddress remoteEndpoint)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic long sendTo(Serializable data, java.net.InetSocketAddress remoteEndpoint) throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic <Type extends Serializable> Future<Type> receiveAsync(Type data)
public <Type extends Serializable> Future<UDPSocket.Packet> receiveAsyncFrom(Type data)
public void receive(Serializable data) throws java.lang.Throwable
java.lang.Throwablepublic UDPSocket.Packet receiveFrom(Serializable data) throws java.lang.Throwable
java.lang.Throwable