Open In App

Node.js socket.remoteAddress() Method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The socket.remoteAddress() method is an inbuilt application programming interface of class Socket within dgram module which is used to get the remote address of the server containing the port, address, and family.

Syntax:

const socket.remoteAddress()

Parameters: This method does not accept any parameters.

Return Value: This method returns the remote address of the server containing the port, address, and family.

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

Filename: index.js

Javascript




// Node.js program to demonstrate the
// server.remoteAddress() 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, () => {
 
    // Connecting server with the port
    // and local host
    server.connect(1234, "localhost", () => {
        console.log("connected");
 
        // Getting remote address of server
        // by using remoteAddress() method
        const add = server.remoteAddress();
 
        // Display the result
        console.log(add);
    });
});
 
// Client sending message to server
client.send("Hello", 0, 7, 1234, "localhost");


Output:

server listening 0.0.0.0:1234
connected
{ address: '127.0.0.1', family: 'IPv4', port: 1234 }

Example 2: If the server is not connected.

Filename: index.js

Javascript




// Node.js program to demonstrate the
// server.remoteAddress() 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 remote address of server
    // by using remoteAddress() method
    // it will throw error
    const add = server.remoteAddress();
 
    // Display the result
    console.log(add);
});
 
// Client sending message to server
client.send("Hello", 0, 7, 1234, "localhost");


Output:

node GFG.js
server listening 0.0.0.0:1234
dgram.js:745
    throw new ERR_SOCKET_DGRAM_NOT_CONNECTED();
    ^

Error [ERR_SOCKET_DGRAM_NOT_CONNECTED]: Not connected
    at Socket.remoteAddress (dgram.js:745:11)
    at Socket.<anonymous> (F:\java\GFG.js:42:28)
    at Socket.onListening (dgram.js:225:10)
    at Socket.emit (events.js:327:22)
    at startListening (dgram.js:150:10)
    at dgram.js:345:7
    at processTicksAndRejections (internal/process/task_queues.js:85:21) {
  code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
}

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_remoteaddress



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads