Skip to content

miktim/UdpSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UdpSocket. Java SE 7+/Android 4+ UDP client and server, MIT (c) 2025 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.0.5"  
  
      Constructors:  
        Constructors create unbounded sockets with reuse enabled, loopback disabled and broadcast on.
        Sets the network interface (may be null) for outgoing multicast datagrams.
        For multicast remote address joins address 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.

        All send/receive methods bind an unbounded socket to the remote port and  
        set the unassigned address/port of the datagram to the address/port of  
        the remote socket.  
  
        InetSocketAddress getRemote();
          - returns remote socket
        UdpSocket bind() throws IOException;
          - binds unbounded socket to remote port
        void send(DatagramPacket dp) throws IOException;
          - send datagram
        void send(byte[] buf) throws IOException;
          - send buffer
        void receive(DatagramPacket dp) throws IOException;
          - wait datagram
            
        UdpSocket setPayloadSize(int size);
          - sets max size of receiving datagram
        int getPayloadSize();
          - default: 1500 bytes
        boolean isReceiving();
          - returns receiving state

        void receive(UdpSocket.Handler handler);
          - calls handler onStart method and starts receiving datagrams
  
        void close();
          - [stops receiving, calls the handler's onClose method,] [leaves initial multicast group,] 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  
  

About

Java 7+ UDP client and server package based on the Java MulticastSocket class

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published