UDP - dgram example in Nodejs
Here’s an example of how to use the dgram
module in Node.js to create a simple UDP server and client. UDP (User Datagram Protocol) is a communication protocol that allows sending messages (datagrams) without establishing a connection, making it faster but less reliable than TCP.
UDP Server
First, create a UDP server that listens for messages and sends a response back to the client.
udp_server.js:
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
// Event handler for receiving messages
server.on('message', (msg, rinfo) => {
console.log(`Server received: ${msg} from ${rinfo.address}:${rinfo.port}`);
// Sending a response back to the client
const response = Buffer.from('Hello from the server!');
server.send(response, rinfo.port, rinfo.address, (err) => {
if (err) {
console.error('Error sending response:', err);
}
});
});
// Event handler for server listening
server.on('listening', () => {
const address = server.address();
console.log(`Server listening on ${address.address}:${address.port}`);
});
// Bind the server to a specific port and address
server.bind(41234, 'localhost');
UDP Client
Next, create a UDP client that sends a message to the server and handles the response.
udp_client.js:
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
// Message to be sent to the server
const message = Buffer.from('Hello from the client!');
// Event handler for receiving messages from the server
client.on('message', (msg, rinfo) => {
console.log(`Client received: ${msg} from ${rinfo.address}:${rinfo.port}`);
// Closing the client after receiving the response
client.close();
});
// Sending the message to the server
client.send(message, 41234, 'localhost', (err) => {
if (err) {
console.error('Error sending message:', err);
client.close();
} else {
console.log('Message sent to server');
}
});
How to Run the Example
-
Start the UDP Server: Open a terminal and run the server script:
node udp_server.js
-
Run the UDP Client: Open another terminal and run the client script:
node udp_client.js
Expected Output
Server Output:
Server listening on 127.0.0.1:41234
Server received: Hello from the client! from 127.0.0.1:some_random_port
Client Output:
Message sent to server
Client received: Hello from the server! from 127.0.0.1:41234
Explanation
-
Server (
udp_server.js
):- The server is created using
dgram.createSocket('udp4')
for IPv4. - It listens for incoming messages on port
41234
. - When a message is received, it logs the message and the sender's address and port.
- It then sends a response back to the client.
- The server is created using
-
Client (
udp_client.js
):- The client is also created using
dgram.createSocket('udp4')
. - It sends a message to the server at port
41234
. - When it receives a response from the server, it logs the response and then closes the client.
- The client is also created using
This example demonstrates the basic usage of the dgram
module for UDP communication in Node.js, showcasing how to set up a simple server and client to send and receive messages.