socket.address() Method in Node.js
×


socket.address() Method in Node.js

681

In network programming with Node.js, it's often important to retrieve details about a socket connection, such as the port number or IP address the socket is using. This is where the socket.address() method comes in. It's a useful method that provides metadata about the socket’s binding, whether it’s a TCP or UDP connection.

What is socket.address()?

The socket.address() method is used to get the address details of a socket once it has been bound to a port. It returns an object that includes the IP address, port number, and the address family (e.g., IPv4 or IPv6). This method is available on both TCP and UDP sockets in Node.js.

Syntax


const addressInfo = socket.address();
        

The method returns an object with the following structure:

  • address: The IP address that the socket is bound to.
  • port: The port number the socket is listening on.
  • family: The address family, usually either 'IPv4' or 'IPv6'.

Example with a TCP Server

Here's how you can use socket.address() in a basic TCP server setup:


const net = require('net');

const server = net.createServer((socket) => {
    console.log('New connection received.');
});

server.listen(3000, () => {
    const address = server.address();
    console.log(`Server is listening on ${address.address}:${address.port} (${address.family})`);
});
        

In this example, once the server starts and begins listening on port 3000, we call server.address() to fetch the IP and port it's bound to. This is especially helpful when using dynamic port assignment or when logging server status for debugging or monitoring.

Example with a UDP Socket

You can also use socket.address() with a UDP socket created using the dgram module:


const dgram = require('dgram');
const socket = dgram.createSocket('udp4');

socket.bind(41234, () => {
    const info = socket.address();
    console.log(`UDP socket bound to ${info.address}:${info.port} (${info.family})`);
});
        

After the socket is bound to a port, we log its address information using socket.address(). This is useful in scenarios where the port is assigned dynamically or needs to be verified programmatically.

When to Use socket.address()

The socket.address() method is helpful in various use cases, such as:

  • Logging the IP and port the server is listening on.
  • Debugging networking behavior in your app.
  • Determining which address a socket is currently using.
  • Validating that binding has occurred successfully.

Conclusion

The socket.address() method in Node.js is a simple yet essential utility when working with sockets. Whether you’re building TCP or UDP servers, this method provides a reliable way to fetch binding information, helping you track and manage your network connections more effectively. It’s a small feature with big impact in real-world applications.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat