Node.js socket.send() Method

The socket.send() method is an inbuilt application programming interface of class Socket within dgram module which is used to send the message from one socket to another.

Syntax:

socket.send(msg[, offset, length][, port][, address][, callback])

Parameters: This method takes the following parameter:

  • msg: Message to be sent.
  • offset: Offset in the buffer where the message starts.
  • length: Number of bytes in the message.
  • port: Destination port.
  • address: Destination hostname or IP address.
  • callback: Called when the message has been sent.

Return Value: This method does not return any value.

Example 1: In this example, we will see the use of socket.send() Method

Filename: index.js

Javascript




// Node.js program to demonstrate the
// server.send() method
 
// Importing dgram module
const dgram = require('dgram');
 
// Creating and initializing client
// and server socket
let client = dgram.createSocket("udp4");
let server = dgram.createSocket("udp4");
 
// Catching the message event
server.on("message", function (msg) {
 
    // Displaying the client message
    process.stdout.write("UDP String: " + msg + "\n");
 
    // Exiting process
    process.exit();
})
 
    // Binding server with port
    .bind(1234, () => {
 
        // Getting the reference of the server
        // by using ref() method
        const size = server.ref();
 
        // Display the result
        console.log(size.eventNames());
    });
 
// Client sending message to server
// by using send() method
client.send("Hello", 0, 7, 1234, "localhost");


Output:

[ 'message' ]
UDP String: Hello

Example 2: In this example, we will see the use of a socket.send() Method

Filename: index.js

Javascript




// Node.js program to demonstrate the
// server.send() method
 
// Importing dgram module
const dgram = require('dgram');
 
// Creating and initializing client
// and server socket
let client = dgram.createSocket("udp4");
let server = dgram.createSocket("udp4");
 
// Catching the message event
server.on("message", function (msg) {
 
    // Displaying the client message
    process.stdout.write("UDP String: " + msg + "\n");
 
    // Exiting process
    process.exit();
});
 
// Catching the listening event
server.on('listening', () => {
 
    // Getting address information for the server
    const address = server.address();
 
    // Display the result
    console.log(
        `server listening ${address.address}:${address.port}`);
});
 
 
// Binding server with port address
// by using bind() method
server.bind(1234, () => {
 
    // Getting the reference of server
    // by using ref() method
    const size = server.ref();
 
    // display the result
    console.log(size.eventNames());
});
 
// Client sending message to server
// by using send() method
client.send("Hello", 0, 7, 1234, "localhost", (err) => {
 
    if (err) throw err;
    console.log("message sent");
});


Output:

server listening 0.0.0.0:1234
[ 'message', 'listening' ]
message sent
UDP String: Hello

Run the index.js file using the following command:

node index.js

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/dgram.html#dgram_socket_send_msg_offset_length_port_address_callback



Contact Us