Skip to main content

UDP connector: Ping

Warning: This is an unstable API that is subject to change or removal at anytime.
An example of writing a 'ping' message to a UDP server on localhost.

Edit on Github
Instantiate an instance of text encoder to write to the UDP stream.
const encoder = new TextEncoder();
Create a UDP listener to allow us to send a ping to the other UDP server.
const listener = Deno.listenDatagram({
  port: 10001,
  transport: "udp",
});
Since UDP is a connectionless protocol, we need to define the address of the listener
const peerAddress: Deno.NetAddr = {
  transport: "udp",
  hostname: "127.0.0.1",
  port: 10000,
};
Encode the 'ping' message and write to the UDP connection for the server to receive.
await listener.send(encoder.encode("ping"), peerAddress);
listener.close();

Run this example locally using the Deno CLI:

deno run --allow-net --unstable-net https://docs.deno.com/examples/udp-connector.ts