Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SocketUDP

Represents a UDP Datagram.

Hierarchy

Index

Constructors

constructor

  • new SocketUDP(portFrom?: undefined | number, hostFrom?: undefined | string, ipVersion?: "IPv4" | "IPv6"): SocketUDP
  • Creates a UDP socket datagram with an address to use as the default socket to send/receive from. Because UDP is connection-less unlike TCP, no Error is thrown on construction if the host/port combo do no listen.

    Parameters

    • Optional portFrom: undefined | number

      An optional local port to bind to. If left undefined then the local port of the socket is chosen by operating system.

    • Optional hostFrom: undefined | string

      An optional address to bind to. If left undefined, empty string, or "*", then the operating system attempts to bind to all local addresses.

    • Optional ipVersion: "IPv4" | "IPv6"

      The IP version to be used. IPv4 by default.

    Returns SocketUDP

Properties

Readonly hostFrom

hostFrom: string

The socket local address. Empty string means any bound host.

isBlocking

isBlocking: boolean

Gets/sets the blocking nature of the Socket. True if to block, false otherwise.

When a socket is blocking, calls such as receive() and accept() will synchronously wait until there is data to return from those calls. When a socket is not set to block, they will check and immediately return undefined when there is no data from those calls.

Readonly isDestroyed

isDestroyed: boolean

Flag if this socket has been manually disconnected and thus destroyed. Destroyed sockets cannot be re-used. True if this socket has been destroyed and disconnected. False otherwise.

NOTE: On unexpected socket errors this may not be set correctly. This check can only ensure this socket disconnected in an expected fashion.

Readonly isIPv4

isIPv4: boolean

Flag if the socket is Internet Protocol Version 4 (IPv4).

Readonly isIPv6

isIPv6: boolean

Flag if the socket is Internet Protocol Version 6 (IPv6).

Readonly portFrom

portFrom: number

The local port the socket is bound to.

Methods

disconnect

  • disconnect(): void
  • Disconnects this so. Once this is called the socket is considered "destroyed" and no no longer be used for any form of communication.

    Returns void

receiveFrom

  • receiveFrom(): { data: Buffer; host: string; port: number } | undefined
  • Receive data from datagrams and returns the data and their address.

    Returns { data: Buffer; host: string; port: number } | undefined

    An object, containing the key data as a Buffer of the received data. The address is present as key host and key port.

sendTo

  • sendTo(hostTo: string, portTo: number, data: string | Buffer | Uint8Array): void
  • Sends to a specific datagram address some data.

    Parameters

    • hostTo: string

      The host string to send data to.

    • portTo: number

      The port number to send data to.

    • data: string | Buffer | Uint8Array

      The actual data payload to send. Can be a string, Buffer, or Uint8Array.

    Returns void

Generated using TypeDoc