Intermediate
cli
UDP Connector: Ping
An example of writing a 'ping' message to a UDP server on localhost.
Instantiate an instance of text encoder to write to the UDP stream.
!--frsh-copybutton:1-->
const encoder = new TextEncoder();
Create a UDP listener to allow us to send a ping to the other UDP server.
!--frsh-copybutton:2-->
const listener = await Deno.listenDatagram({
port: 10001,
transport: "udp",
});
Since UDP is a connectionless protocol, we need to define the address of the listener
!--frsh-copybutton:3-->
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.
!--frsh-copybutton:4-->
await listener.send(encoder.encode("ping"), peerAddress);
listener.close();
Run this example locally using the Deno CLI:
deno run --allow-net --unstable https://byexample-fkzwmawz24cg.deno.dev/udp-connector.ts
Additional resources: