java.lang.Object
org.cybergarage.upnp.ssdp.HTTPUSocket
Direct Known Subclasses:
SSDPSearchResponseSocket

public class HTTPUSocketextends Object
UDP socket implementation for SSDP (Simple Service Discovery Protocol) communication.

This class provides UDP socket functionality specifically designed for SSDP operations in UPnP networks. It handles both unicast and multicast UDP communication for device discovery and advertisement.

Key features:

  • UDP unicast socket management
  • IPv4 and IPv6 support
  • Packet sending and receiving
  • Timestamp tracking for received packets
  • Network interface binding

This class is used by SSDP components to send discovery messages (M-SEARCH) and receive device advertisements (NOTIFY messages) over UDP multicast and unicast communication.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private String
    /////////////////////////////////////////////
    /////////////////////////////////////////////
  • Constructor Summary

    Constructors
    Constructor
    Description
    Open a socket bound to any free local port.
    HTTPUSocket(int bindPort)
    Open a socket bound to a given local port on any address.
    HTTPUSocket(String bindAddr, int bindPort)
    Open a socket bound to a given local address and port.
  • Method Summary

    Public Methods
    Modifier and Type
    Method
    Description
    boolean
    Close the socket and clear the reference to it.
    protected void
    Close the socket when the object is garbage collected.
    Open a datagram socket on an ephemeral port, for SSDP unicast traffic.
    Return the local address this socket reports.
    Return the underlying datagram socket, for callers that need it directly.
    boolean
    Open a datagram socket on an ephemeral port, replacing any open socket.
    boolean
    open(int bindPort)
    Open a datagram socket bound to a local port with address reuse allowed.
    boolean
    open(String bindAddr, int bindPort)
    Open a datagram socket bound to a local address and port.
    boolean
    post(String addr, int port, String msg)
    Send one SSDP message as a UDP datagram.
    Block until one datagram arrives, then wrap it as an SSDP packet.
    void
    Override the address this socket reports as its own.
    Return the local address this socket reports.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • ssdpUniSock

      private DatagramSocket ssdpUniSock
      /////////////////////////////////////////////
    • localAddr

      private String localAddr
      /////////////////////////////////////////////
  • Constructor Details

    • HTTPUSocket

      public HTTPUSocket()
      Open a socket bound to any free local port.
    • HTTPUSocket

      public HTTPUSocket(String bindAddr, int bindPort)
      Open a socket bound to a given local address and port.
      Parameters:
      bindAddr - the local address to bind the socket to, resolved by name
      bindPort - the local UDP port to bind, 0 for any free port
    • HTTPUSocket

      public HTTPUSocket(int bindPort)
      Open a socket bound to a given local port on any address.
      Parameters:
      bindPort - the local UDP port to bind, 0 for any free port
  • Method Details

    • getDatagramSocket

      public DatagramSocket getDatagramSocket()
      Open a datagram socket on an ephemeral port, for SSDP unicast traffic.
      Returns:
      the socket SSDP datagrams are sent and received on, null if none is open
    • finalize

      protected void finalize()
      Close the socket when the object is garbage collected.
    • setLocalAddress

      public void setLocalAddress(String addr)
      Override the address this socket reports as its own.
      Parameters:
      addr - the address to report for this socket instead of the one it bound to
    • getUDPSocket

      public DatagramSocket getUDPSocket()
      Return the underlying datagram socket, for callers that need it directly.
      Returns:
      the open DatagramSocket, or null if the socket is closed
    • getLocalAddress

      public String getLocalAddress()
      Return the local address this socket reports.
      Returns:
      the address set by setLocalAddress(String), else the bound address, or "" if closed
    • open

      public boolean open()
      Open a datagram socket on an ephemeral port, replacing any open socket.
      Returns:
      true if the socket was created, false if the bind failed
    • open

      public boolean open(String bindAddr, int bindPort)
      Open a datagram socket bound to a local address and port.
      Parameters:
      bindAddr - the local address to bind the socket to, resolved by name
      bindPort - the local UDP port to bind, 0 for any free port
      Returns:
      true if the socket bound to the address and port, false if either failed
    • open

      public boolean open(int bindPort)
      Open a datagram socket bound to a local port with address reuse allowed.
      Parameters:
      bindPort - the local UDP port to bind, 0 for any free port
      Returns:
      true if the socket bound with address reuse, false if the bind failed
    • close

      public boolean close()
      Close the socket and clear the reference to it.
      Returns:
      true if the socket is now closed, false if closing it failed
    • post

      public boolean post(String addr, int port, String msg)
      Send one SSDP message as a UDP datagram.
      Parameters:
      addr - the address to send to, resolved by name
      port - the UDP port to send to
      msg - the SSDP message to send, encoded as UTF-8
      Returns:
      true if the datagram was sent, false if the send failed
    • receive

      public SSDPPacket receive()
      Block until one datagram arrives, then wrap it as an SSDP packet.
      Returns:
      the packet that arrived, or null if the receive failed
    • toString

      public String toString()
      Return the local address this socket reports.
      Overrides:
      toString in class Object
      Returns:
      the address set by setLocalAddress(), which is empty when unset