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 clinet
// 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");
输出:
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 clinet
// 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.<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' }
使用以下命令运行index.js文件:
node index.js
参考:https://nodejs.org/dist/latest-v12.x/docs/api/dgram.html#dgram_socket_remoteaddress
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js fs.fsyncSync()用法及代码示例
- Node.js process.nextTick()用法及代码示例
- Node.js GM charcoal()用法及代码示例
- Node.js GM blur()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM drawLine()用法及代码示例
- Node.js GM drawArc()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
- Node.js GM drawBezier()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js socket.remoteAddress() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。