io.vertx.core.datagram

## Interface DatagramSocket

- All Superinterfaces:
    Measured, ReadStream<DatagramPacket>, StreamBase

      public interface DatagramSocket
      extends ReadStream<DatagramPacket>, Measured

  A datagram socket can be used to send `DatagramPacket`'s to remote datagram servers and receive `DatagramPacket`s .

  Usually you use a datagram socket to send UDP over the wire. UDP is connection-less which means you are not connected to the remote peer in a persistent way. Because of this you have to supply the address and port of the remote peer when sending data.

  You can send data to ipv4 or ipv6 addresses, which also include multicast addresses.

  Please consult the documentation for more information on datagram sockets.

  Author:
    Norman Maurer

- - ### Method Summary

    All Methods Instance Methods Abstract Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `Future<Void>` | `blockMulticastGroup(String multicastAddress, String sourceToBlock)` Like `blockMulticastGroup(String, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `blockMulticastGroup(String multicastAddress, String sourceToBlock, Handler<AsyncResult<Void>> handler)` Block the given address for the given multicast address and notifies the `Handler` once the operation completes.
    `Future<Void>` | `blockMulticastGroup(String multicastAddress, String networkInterface, String sourceToBlock)` Like `blockMulticastGroup(String, String, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `blockMulticastGroup(String multicastAddress, String networkInterface, String sourceToBlock, Handler<AsyncResult<Void>> handler)` Block the given address for the given multicast address on the given network interface and notifies the `Handler` once the operation completes.
    `Future<Void>` | `close()` Closes the `DatagramSocket`.
    `void` | `close(Handler<AsyncResult<Void>> handler)` Closes the `DatagramSocket` implementation asynchronous and notifies the handler once done.
    `DatagramSocket` | `endHandler(Handler<Void> endHandler)` Deprecated. removed in Vert.x 5
    `DatagramSocket` | `exceptionHandler(Handler<Throwable> handler)` Set an exception handler on the read stream.
    `DatagramSocket` | `fetch(long amount)` Deprecated. no replacement for back-pressure, instead ignore the packet or buffer it somewhere
    `DatagramSocket` | `handler(Handler<DatagramPacket> handler)` Set a data handler.
    `Future<DatagramSocket>` | `listen(int port, String host)` Like `listen(int, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `listen(int port, String host, Handler<AsyncResult<DatagramSocket>> handler)` Start listening on the given port and host.
    `Future<Void>` | `listenMulticastGroup(String multicastAddress)` Like `listenMulticastGroup(String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `listenMulticastGroup(String multicastAddress, Handler<AsyncResult<Void>> handler)` Joins a multicast group and listens for packets send to it.
    `Future<Void>` | `listenMulticastGroup(String multicastAddress, String networkInterface, String source)` Like `listenMulticastGroup(String, String, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `listenMulticastGroup(String multicastAddress, String networkInterface, String source, Handler<AsyncResult<Void>> handler)` Joins a multicast group and listens for packets send to it on the given network interface.
    `SocketAddress` | `localAddress()` Return the `SocketAddress` to which this `DatagramSocket` is bound.
    `DatagramSocket` | `pause()` Deprecated. no replacement for back-pressure, instead ignore the packet or buffer it somewhere
    `DatagramSocket` | `resume()` Deprecated. no replacement for back-pressure, instead ignore the packet or buffer it somewhere
    `Future<Void>` | `send(Buffer packet, int port, String host)` Like `send(Buffer, int, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `send(Buffer packet, int port, String host, Handler<AsyncResult<Void>> handler)` Write the given `Buffer` to the `SocketAddress`.
    `Future<Void>` | `send(String str, int port, String host)` Like `send(String, int, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `send(String str, int port, String host, Handler<AsyncResult<Void>> handler)` Write the given `String` to the `SocketAddress` using UTF8 encoding.
    `Future<Void>` | `send(String str, String enc, int port, String host)` Like `send(String, String, int, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `send(String str, String enc, int port, String host, Handler<AsyncResult<Void>> handler)` Write the given `String` to the `SocketAddress` using the given encoding.
    `WriteStream<Buffer>` | `sender(int port, String host)` Returns a `WriteStream<Buffer>` able to send `Buffer` to the `SocketAddress`.
    `Future<Void>` | `unlistenMulticastGroup(String multicastAddress)` Like `unlistenMulticastGroup(String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `unlistenMulticastGroup(String multicastAddress, Handler<AsyncResult<Void>> handler)` Leaves a multicast group and stops listening for packets send to it.
    `Future<Void>` | `unlistenMulticastGroup(String multicastAddress, String networkInterface, String source)` Like `unlistenMulticastGroup(String, String, String, Handler)` but returns a `Future` of the asynchronous result
    `DatagramSocket` | `unlistenMulticastGroup(String multicastAddress, String networkInterface, String source, Handler<AsyncResult<Void>> handler)` Leaves a multicast group and stops listening for packets send to it on the given network interface.

    - ### Methods inherited from interface io.vertx.core.streams.ReadStream

      `collect, pipe, pipeTo, pipeTo`

    - ### Methods inherited from interface io.vertx.core.metrics.Measured

      `isMetricsEnabled`

- - ### Method Detail

    - #### send

          DatagramSocket send(Buffer packet,
                              int port,
                              String host,
                              Handler<AsyncResult<Void>> handler)

      Write the given `Buffer` to the `SocketAddress`. The `Handler` will be notified once the write completes.
      Parameters:
        `packet` - the `Buffer` to write
        `port` - the host port of the remote peer
        `host` - the host address of the remote peer
        `handler` - the `Handler` to notify once the write completes.
      Returns:
        a reference to this, so the API can be used fluently

    - #### send

          Future<Void> send(Buffer packet,
                            int port,
                            String host)

      Like `send(Buffer, int, String, Handler)` but returns a `Future` of the asynchronous result

    - #### sender

          WriteStream<Buffer> sender(int port,
                                     String host)

      Returns a `WriteStream<Buffer>` able to send `Buffer` to the `SocketAddress`.
      Parameters:
        `port` - the port of the remote peer
        `host` - the host address of the remote peer
      Returns:
        the write stream for sending packets

    - #### send

          DatagramSocket send(String str,
                              int port,
                              String host,
                              Handler<AsyncResult<Void>> handler)

      Write the given `String` to the `SocketAddress` using UTF8 encoding. The `Handler` will be notified once the write completes.
      Parameters:
        `str` - the `String` to write
        `port` - the host port of the remote peer
        `host` - the host address of the remote peer
        `handler` - the `Handler` to notify once the write completes.
      Returns:
        a reference to this, so the API can be used fluently

    - #### send

          Future<Void> send(String str,
                            int port,
                            String host)

      Like `send(String, int, String, Handler)` but returns a `Future` of the asynchronous result

    - #### send

          DatagramSocket send(String str,
                              String enc,
                              int port,
                              String host,
                              Handler<AsyncResult<Void>> handler)

      Write the given `String` to the `SocketAddress` using the given encoding. The `Handler` will be notified once the write completes.
      Parameters:
        `str` - the `String` to write
        `enc` - the charset used for encoding
        `port` - the host port of the remote peer
        `host` - the host address of the remote peer
        `handler` - the `Handler` to notify once the write completes.
      Returns:
        a reference to this, so the API can be used fluently

    - #### send

          Future<Void> send(String str,
                            String enc,
                            int port,
                            String host)

      Like `send(String, String, int, String, Handler)` but returns a `Future` of the asynchronous result

    - #### close

          void close(Handler<AsyncResult<Void>> handler)

      Closes the `DatagramSocket` implementation asynchronous and notifies the handler once done.
      Parameters:
        `handler` - the handler to notify once complete

    - #### close

          Future<Void> close()

      Closes the `DatagramSocket`. The close itself is asynchronous.

    - #### localAddress

          SocketAddress localAddress()

      Return the `SocketAddress` to which this `DatagramSocket` is bound.
      Returns:
        the socket address

    - #### listenMulticastGroup

          DatagramSocket listenMulticastGroup(String multicastAddress,
                                              Handler<AsyncResult<Void>> handler)

      Joins a multicast group and listens for packets send to it. The `Handler` is notified once the operation completes.
      Parameters:
        `multicastAddress` - the address of the multicast group to join
        `handler` - then handler to notify once the operation completes
      Returns:
        a reference to this, so the API can be used fluently

    - #### listenMulticastGroup

          Future<Void> listenMulticastGroup(String multicastAddress)

      Like `listenMulticastGroup(String, Handler)` but returns a `Future` of the asynchronous result

    - #### listenMulticastGroup

          DatagramSocket listenMulticastGroup(String multicastAddress,
                                              String networkInterface,
                                              String source,
                                              Handler<AsyncResult<Void>> handler)

      Joins a multicast group and listens for packets send to it on the given network interface. The `Handler` is notified once the operation completes.
      Parameters:
        `multicastAddress` - the address of the multicast group to join
        `networkInterface` - the network interface on which to listen for packets.
        `source` - the address of the source for which we will listen for multicast packets
        `handler` - then handler to notify once the operation completes
      Returns:
        a reference to this, so the API can be used fluently

    - #### listenMulticastGroup

          Future<Void> listenMulticastGroup(String multicastAddress,
                                            String networkInterface,
                                            String source)

      Like `listenMulticastGroup(String, String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### unlistenMulticastGroup

          DatagramSocket unlistenMulticastGroup(String multicastAddress,
                                                Handler<AsyncResult<Void>> handler)

      Leaves a multicast group and stops listening for packets send to it. The `Handler` is notified once the operation completes.
      Parameters:
        `multicastAddress` - the address of the multicast group to leave
        `handler` - then handler to notify once the operation completes
      Returns:
        a reference to this, so the API can be used fluently

    - #### unlistenMulticastGroup

          Future<Void> unlistenMulticastGroup(String multicastAddress)

      Like `unlistenMulticastGroup(String, Handler)` but returns a `Future` of the asynchronous result

    - #### unlistenMulticastGroup

          DatagramSocket unlistenMulticastGroup(String multicastAddress,
                                                String networkInterface,
                                                String source,
                                                Handler<AsyncResult<Void>> handler)

      Leaves a multicast group and stops listening for packets send to it on the given network interface. The `Handler` is notified once the operation completes.
      Parameters:
        `multicastAddress` - the address of the multicast group to join
        `networkInterface` - the network interface on which to listen for packets.
        `source` - the address of the source for which we will listen for multicast packets
        `handler` - the handler to notify once the operation completes
      Returns:
        a reference to this, so the API can be used fluently

    - #### unlistenMulticastGroup

          Future<Void> unlistenMulticastGroup(String multicastAddress,
                                              String networkInterface,
                                              String source)

      Like `unlistenMulticastGroup(String, String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### blockMulticastGroup

          DatagramSocket blockMulticastGroup(String multicastAddress,
                                             String sourceToBlock,
                                             Handler<AsyncResult<Void>> handler)

      Block the given address for the given multicast address and notifies the `Handler` once the operation completes.
      Parameters:
        `multicastAddress` - the address for which you want to block the source address
        `sourceToBlock` - the source address which should be blocked. You will not receive an multicast packets for it anymore.
        `handler` - the handler to notify once the operation completes
      Returns:
        a reference to this, so the API can be used fluently

    - #### blockMulticastGroup

          Future<Void> blockMulticastGroup(String multicastAddress,
                                           String sourceToBlock)

      Like `blockMulticastGroup(String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### blockMulticastGroup

          DatagramSocket blockMulticastGroup(String multicastAddress,
                                             String networkInterface,
                                             String sourceToBlock,
                                             Handler<AsyncResult<Void>> handler)

      Block the given address for the given multicast address on the given network interface and notifies the `Handler` once the operation completes.
      Parameters:
        `multicastAddress` - the address for which you want to block the source address
        `networkInterface` - the network interface on which the blocking should occur.
        `sourceToBlock` - the source address which should be blocked. You will not receive an multicast packets for it anymore.
        `handler` - the handler to notify once the operation completes
      Returns:
        a reference to this, so the API can be used fluently

    - #### blockMulticastGroup

          Future<Void> blockMulticastGroup(String multicastAddress,
                                           String networkInterface,
                                           String sourceToBlock)

      Like `blockMulticastGroup(String, String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### listen

          DatagramSocket listen(int port,
                                String host,
                                Handler<AsyncResult<DatagramSocket>> handler)

      Start listening on the given port and host. The handler will be called when the socket is listening.
      Parameters:
        `port` - the port to listen on
        `host` - the host to listen on
        `handler` - the handler will be called when listening
      Returns:
        a reference to this, so the API can be used fluently

    - #### listen

          Future<DatagramSocket> listen(int port,
                                        String host)

      Like `listen(int, String, Handler)` but returns a `Future` of the asynchronous result

    - #### pause

          @Deprecated
          DatagramSocket pause()

      Deprecated. no replacement for back-pressure, instead ignore the packet or buffer it somewhere
      Description copied from interface: `ReadStream`
      Pause the `ReadStream`, it sets the buffer in `fetch` mode and clears the actual demand.

      While it's paused, no data will be sent to the data `handler`.

      Specified by:
        `pause` in interface `ReadStream<DatagramPacket>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### resume

          @Deprecated
          DatagramSocket resume()

      Deprecated. no replacement for back-pressure, instead ignore the packet or buffer it somewhere
      Description copied from interface: `ReadStream`
      Resume reading, and sets the buffer in `flowing` mode.

      If the `ReadStream` has been paused, reading will recommence on it.
      Specified by:
        `resume` in interface `ReadStream<DatagramPacket>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### fetch

          @Deprecated
          DatagramSocket fetch(long amount)

      Deprecated. no replacement for back-pressure, instead ignore the packet or buffer it somewhere
      Description copied from interface: `ReadStream`
      Fetch the specified `amount` of elements. If the `ReadStream` has been paused, reading will recommence with the specified `amount` of items, otherwise the specified `amount` will be added to the current stream demand.
      Specified by:
        `fetch` in interface `ReadStream<DatagramPacket>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### endHandler

          @Deprecated
          DatagramSocket endHandler(Handler<Void> endHandler)

      Deprecated. removed in Vert.x 5
      Description copied from interface: `ReadStream`
      Set an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.
      Specified by:
        `endHandler` in interface `ReadStream<DatagramPacket>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### handler

          DatagramSocket handler(Handler<DatagramPacket> handler)

      Description copied from interface: `ReadStream`
      Set a data handler. As data is read, the handler will be called with the data.
      Specified by:
        `handler` in interface `ReadStream<DatagramPacket>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### exceptionHandler

          DatagramSocket exceptionHandler(Handler<Throwable> handler)

      Description copied from interface: `ReadStream`
      Set an exception handler on the read stream.
      Specified by:
        `exceptionHandler` in interface `ReadStream<DatagramPacket>`
      Specified by:
        `exceptionHandler` in interface `StreamBase`
      Parameters:
        `handler` - the exception handler
      Returns:
        a reference to this, so the API can be used fluently

Copyright © 2025 Eclipse. All rights reserved.