📜  Node.js socket.connect() 方法

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

Node.js socket.connect() 方法

socket.connect() 方法是 dgram 模块中 Socket 类的内置应用程序编程接口,用于将特定服务器连接到特定端口和地址。

句法:

const socket.connect(port[, address][, callback]) 

参数:此方法接受以下参数:

  • port:此参数保存端口号。
  • 地址:该参数保存服务器地址信息。
  • callback:此参数保存回调函数以供进一步操作。

返回值:此方法不返回任何值。

示例 1:文件名:index.js

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


Javascript
// Node.js program to demonstrate the
// server.connect() 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, () => {
 
    // Adding a multicast address for others to join
    server.addMembership('224.0.0.114');
 
});
 
 
// Connecting the server with particular local host
// and address by using connect() method
server.connect(1234, "localhost", () => {
    console.log("connected")
})
 
// Client sending message to server
client.send("Hello", 0, 7, 1234, "localhost");


输出:

{ address: '0.0.0.0', family: 'IPv4', port: 1234 }
connected
UDP String: Hello

示例 2:文件名:index.js

Javascript

// Node.js program to demonstrate the
// server.connect() 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, () => {
 
    // Adding a multicast address for others to join
    server.addMembership('224.0.0.114');
 
});
 
 
// Connecting the server with particular local host
// and address by using connect() method
server.connect(1234, "localhost", () => {
    console.log("connected")
})
 
// Client sending message to server
client.send("Hello", 0, 7, 1234, "localhost");

输出:

server listening 0.0.0.0:1234
connected
UDP String: Hello

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

node index.js

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