📜  Node.js socket.remoteAddress() 方法

📅  最后修改于: 2022-05-13 01:56:23.794000             🧑  作者: Mango

Node.js socket.remoteAddress() 方法

socket.remoteAddress()方法是 dgram 模块中 Socket 类的内置应用程序编程接口,用于获取服务器的远程地址,包含端口、地址和族。

句法:

const socket.remoteAddress()

参数:此方法不接受任何参数。

返回值:该方法返回服务器的远程地址,包含端口、地址和族。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// server.remoteAddress() method
 
// Importing dgram module
var dgram = require('dgram');
 
// Creating and initializing client
// and server socket
var client = dgram.createSocket("udp4");
var 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");


Javascript
// Node.js program to demonstrate the
// server.remoteAddress() method
 
// Importing dgram module
var dgram = require('dgram');
 
// Creating and initializing client
// and server socket
var client = dgram.createSocket("udp4");
var 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");


输出:

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

示例 2:如果服务器未连接。

文件名:index.js

Javascript

// Node.js program to demonstrate the
// server.remoteAddress() method
 
// Importing dgram module
var dgram = require('dgram');
 
// Creating and initializing client
// and server socket
var client = dgram.createSocket("udp4");
var 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");

输出:

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. (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'
}

使用以下命令运行 index.js 文件:

node index.js

参考: https://nodejs.org/dist/latest-v12.x/docs/api/dgram.html#dgram_socket_remoteaddress