public class UDP
extends java.lang.Object
implements java.lang.Runnable
The socket type is define at his initialyzation by the passed IP address. To reach a host (interface) within a network, you need to specified the kind of address:
To perform actions on receive and/or timeout events, you must implement
specific method(s) in your code. This method will be automatically called
when the socket receive incoming data or a timeout event. By default, the
"receive handler" is typically receive(byte[] data)
but you can
retrieve more informations about the datagram packet, see
setReceiveHandler(String name)
for more informations. In the same
logic, the default "timeout handler" is explicitly timeout()
.
note: currently applets are not allowed to use multicast sockets
Modifier and Type | Field and Description |
---|---|
static int |
BUFFER_SIZE
The default socket buffer length in bytes.
|
Constructor and Description |
---|
UDP(java.lang.Object owner)
Create a new datagram socket and binds it to an available port and every
address on the local host machine.
|
UDP(java.lang.Object owner,
int port)
Create a new datagram socket and binds it to the specified port on the
local host machine.
|
UDP(java.lang.Object owner,
int port,
java.lang.String ip)
Create a new datagram socket and binds it to the specified port on the
specified local address or multicast group address.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
address()
Return the actual socket's local address, or
null if the
address correspond to any local address. |
boolean |
broadcast(boolean on)
Enables or disables the ability of the current process to send broadcast
messages.
|
void |
close()
Close the actuel datagram socket and all associate resources.
|
void |
dispose()
Close the socket.
|
int |
getBuffer()
Return the actual socket buffer length
|
int |
getTimeToLive()
Return the "Time to Live" value or -1 if an error occurred (or if
the current socket is not a multicast socket).
|
boolean |
isBroadcast()
Returns whether the opened socket send broadcast message socket or not.
|
boolean |
isClosed()
Returns whether the current socket is closed or not.
|
boolean |
isJoined()
Returns whether the multicast socket is joined to a group address.
|
boolean |
isListening()
Returns whether the socket wait for incoming data or not.
|
boolean |
isLoopback()
Returns whether the multicast socket loopback mode is enable or not.
|
boolean |
isMulticast()
Returns whether the opened datagram socket is a multicast socket or not.
|
void |
listen()
Wait for incoming data, and call the appropriate handlers each time a
message is received.
|
void |
listen(boolean on)
Start/stop waiting constantly for incoming data.
|
void |
listen(int millis)
Set the socket reception timeout and wait one time for incoming data.
|
void |
log(boolean on)
Enable or disable output process log.
|
void |
loopback(boolean on)
Enable or disable the multicast socket loopback mode.
|
int |
port()
Return the actual socket's local port, or -1 if the socket is closed.
|
void |
run()
Wait for incoming datagram packets.
|
boolean |
send(byte[] buffer)
Send data to the current socket.
|
boolean |
send(byte[] buffer,
java.lang.String ip)
Send data to the requested IP address, to the current socket port.
|
boolean |
send(byte[] buffer,
java.lang.String ip,
int port)
Send data to the requested IP address and port.
|
boolean |
send(java.lang.String message)
Send message to the current socket.
|
boolean |
send(java.lang.String message,
java.lang.String ip)
Send message to the requested IP address, to the current socket port.
|
boolean |
send(java.lang.String message,
java.lang.String ip,
int port)
Send message to the requested IP address and port.
|
boolean |
setBuffer(int size)
Set the maximum size of the packet that can be sent or receive on the
current socket.
|
void |
setReceiveHandler(java.lang.String name)
Register the target's receive handler.
|
void |
setTimeoutHandler(java.lang.String name)
Register the target's timeout handler.
|
boolean |
setTimeToLive(int ttl)
Control the life-time of a datagram in the network for multicast packets
in order to indicates the scope of the multicasts (ie how far the packet
will travel).
|
public static final int BUFFER_SIZE
public UDP(java.lang.Object owner)
owner
- the target object to be call by the receive handlerpublic UDP(java.lang.Object owner, int port)
Pass zero
as port number, will let the system choose an
available port.
owner
- the target object to be call by the receive handlerport
- local port to bindpublic UDP(java.lang.Object owner, int port, java.lang.String ip)
Pass zero
as port number, will let the system choose an
available port. The absence of an address, explicitly null
as IP address will assign the socket to the Unspecified Address (Also
called anylocal or wildcard address). To set up the socket as multicast
socket, pass the group address to be joined. If this address is not a
valid multicast address, a broadcast socket will be created by default.
owner
- the target object to be call by the receive handlerport
- local port to bindip
- host address or group addresspublic void dispose()
close()
public void close()
public boolean isClosed()
public int port()
public java.lang.String address()
null
if the
address correspond to any local address.public boolean send(java.lang.String message)
message
- the message to be sendsend(String message, String ip)
,
send(String message, String ip, int port)
public boolean send(byte[] buffer)
buffer
- data to be sendsend(byte[] data, String ip)
,
send(byte[] data, String ip, int port)
public boolean send(java.lang.String message, java.lang.String ip)
message
- the message to be sendip
- the destination host's IP addresssend(String message)
,
send(String message, String ip, int port)
public boolean send(byte[] buffer, java.lang.String ip)
buffer
- data to be sendip
- the destination host's IP addresssend(byte[] buffer)
,
send(byte[] buffer, String ip, int port)
public boolean send(java.lang.String message, java.lang.String ip, int port)
A null
IP address will assign the packet address to the
local host. Use this method to send message to another application by
passing null
as the destination address.
message
- the message to be sendip
- the destination host's IP addressport
- the destination host's portsend(String message)
,
send(String message, String ip)
public boolean send(byte[] buffer, java.lang.String ip, int port)
A null
IP address will assign the packet address to the
local host. Use this method to send data to another application by
passing null
as the destination address.
buffer
- data to be sendip
- the destination host's IP addressport
- the destination host's portsend(byte[] buffer, String ip)
,
send(byte[] buffer, String ip, int port)
public boolean setBuffer(int size)
return true
if the new buffer value have been correctly set,
false
otherwise.
note : this method has no effect if the socket is listening. To define a new buffer size, call this method before implementing a new buffer in memory. Explicitly before calling a receive methods.
size
- the buffer size value in bytes or n<=0getBuffer()
public int getBuffer()
setBuffer(int size)
public boolean isListening()
public void listen(boolean on)
on
- the required listening status.listen()
,
listen(int millis)
,
setReceiveHandler(String name)
public void listen(int millis)
millis
- the required timeout value in milliseconds.listen()
,
listen(boolean on)
public void listen()
This method force the current Thread
to be ceased for a
temporary period. If you prefer listening without blocking the current
thread, use the listen(int millis)
or
listen(boolean on)
method instead.
public void run()
run
in interface java.lang.Runnable
public void setReceiveHandler(java.lang.String name)
By default, this method name is "receive" with one argument
representing the received data as byte[]
. For more
flexibility, you can change this method with another useful method by
passing his name. You could get more informations by implementing two
additional arguments, a String
representing the sender IP
address and a int
representing the sender port :
void myCustomReceiveHandler(byte[] message, String ip, int port) { // do something here... }
name
- the receive handler namesetTimeoutHandler(String name)
public void setTimeoutHandler(java.lang.String name)
name
- the timeout handler namesetReceiveHandler(String name)
public boolean isMulticast()
public boolean isJoined()
public boolean isBroadcast()
public boolean broadcast(boolean on)
public void loopback(boolean on)
on
- local loopback of multicast datagramspublic boolean isLoopback()
public boolean setTimeToLive(int ttl)
The TTL value must be in range of 0 to 255 inclusive. The larger the number, the farther the multicast packets will travel (by convention):
The default value is 1, meaning that the datagram will not go beyond the local subnet.0 -> restricted to the same host 1 -> restricted to the same subnet (default) <32 -> restricted to the same site <64 -> restricted to the same region <128 -> restricted to the same continent <255 -> no restriction
return true
if no error occured.
ttl
- the "Time to Live" valuegetTimeToLive()
public int getTimeToLive()
setTimeToLive(int ttl)
public void log(boolean on)