miktim/UdpSocket
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
UdpSocket. Java SE 7+/Android 4+ UDP client and server, MIT (c) 2025-2026 miktim@mail.ru
Extends the Java MulticastSocket class
Advantage of MulticastSocket class:
- this class is supported by old and current versions of Java and Android.
Disadvantage:
- does not support source-specific multicast (see Java MulticastChannel class).
Notes:
- the UDP protocol does not guarantee datagram delivery, ordering,
or duplicate protection;
- the maximum safe UDP payload size is ~508 bytes;
- don't forget to open the required UDP port in your firewall.
The jar ./dist/udpsocket-... file was generated with debugging info using JDK1.8 for target JRE1.7
package org.miktim.udpsocket
Class UdpSocket extends MulticastSocket implements Closeable, AutoCloseable
Constants:
String VERSION = "4.2.1"
Constructors:
Constructors create unbounded sockets with reuse enabled,
broadcast on, loopback disabled and TTL=1.
Sets the network interface (may be null) for the remote
multicast address and joins the multicast group.
UdpSocket(InetSocketAddress remoteSoc, NetworkInterface netIf) throws IOException;
UdpSocket(InetAddress remoteAddr, int remotePort, String netIfName) throws IOException;
Methods:
static void isAvailable(int port);
- checks whether the port is available. Not very reliable.
boolean isMulticast();
- returns true if the remote address is a multicast address
InetSocketAddress getRemote();
- returns remote socket address
InetSocketAddress getLocal() throws IOException;
- returns socket address based on remote port, interface
and remote address type (IPv4/IPv6)
See also: java.net.MulticastSocket.getInterface()
UdpSocket bind() throws IOException;
- binds unbound socket to the remote port
UdpSocket setPayloadSize(int size);
- sets the size of the DatagramPacket's buffer for received datagrams
int getPayloadSize();
- default: 1500 bytes
Receiving methods bind an unbound socket to the remote port.
Sending methods set the unassigned address/port of the datagram
to the address/port of the remote socket.
void send(DatagramPacket dp) throws IOException;
- send datagram
void send(byte[] buf) throws IOException;
- send bytes
void send(byte[] buf, int off, int len) throws IOException;
- send bytes
DatagramPacket receive() throws IOException;
- wait datagram;
void receive(DatagramPacket dp) throws IOException;
- wait datagram
See also: java.net.DatagramSocket.setSoTimeout(int timeout)
void receive(UdpSocket.Handler handler);
- calls handler onStart method and starts receiving datagrams
boolean isReceiving();
- returns receiving state
void close();
- [stops receiving,] [leaves initial multicast group,]
[calls the handler's onClose method,] closes socket
interface UdpSocket.Handler {
void onStart(UdpSocket us);
void onError(UdpSocket us, Exception e);
- the socket will be closed after the method exits
void onPacket(UdpSocket us, DatagramPacket dp);
void onClose(UdpSocket us);
- called before closing datagram socket
}
String toString();
- returns socket info
See also inherited DatagramSocket and MulticastSocket methods:
https://docs.oracle.com/javase/7/docs/api/java/net/DatagramSocket.html
https://docs.oracle.com/javase/7/docs/api/java/net/MulticastSocket.html
https://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html
Android Multicast Troubleshooting:
https://stackoverflow.com/questions/13221736/android-device-not-receiving-multicast-package
https://stackoverflow.com/questions/27917605/android-multicastsocket-joingroup-doesnt-trigger-sending-igmp-message