Node.js net.SocketAddress() Method
Last Updated :
28 Apr, 2025
Node.js Net module allows you to create TCP or IPC servers and clients. A TCP client initiates a connection request to a TCP server to establish a connection with the server.
Syntax: Below is the syntax for importing the Net module into your node js project:
const <Variable_Name> = require('node:net');
const Net_Module = require('node:net');
The net.SocketAddress() class provides us with the detailed address of the socket of the network. See the below syntax for calling the SocketAddress() constructor of the class:
Syntax:
const socket = new Net_Module.SocketAddress([options]);
Parameters:
- Options: It consists of the IP address, port, family, and flow label values. It is <object> type, and optional.
Return Value:
- socket.address: <string> Â Returns the IP address.
- socket.family: Â <string> Returns the family of IP address - ipv4/ipv6. Â Â
- socket.flowlabel : Â <number> Returns a number that is an ipv6 flow label.
- socket.port: <number> Returns the port number.
Steps to create Node.js Project:
Step 1: Run the following command in your terminal to set up your Node.js project package.json:
npm init
Step 2: Create an app.js file that describes the code.
Step 3: Now import the net module into your project. and start writing your code.
In case the net module is not installed in your system, you may use the below command to install the net module into the project:
npm i net
Example 1: Let's create an object of the net.SocketAddress() class and print the object.
JavaScript
// Program to create an object of SocketAddress class
// Importing the Net Module into project.
const Net_Module = require('node:net');
// Creating an object by calling the constructor
// of the class
const object = new Net_Module.SocketAddress();
// Printing the object
console.log(object);
Output:
SocketAddress {
address: '127.0.0.1',
port: 0,
family: 'ipv4',
flowlabel: 0
}
As you can see from the output, the object of the class consists of four properties and returns the default values for each of the properties.Â
Example 2: We are creating a custom option object and passing it to this constructor to know whether it changes the default values for all the properties or not.
JavaScript
// Program to create an object of SocketAddress class
// Importing the Net Module into project.
const Net_Module = require('node:net');
// Custom options object
const options = {
address:'142.38.75.36',
family:'ipv4',
port:200
};
// Creating the object by calling the constructor
// of the class and passing the custom options
// object to it
const object = new Net_Module.SocketAddress(options);
// Printing the object.
console.log(object);
Output:
SocketAddress {
address: '142.38.75.36',
port: 200,
family: 'ipv4',
flowlabel: 0
}
We have used a random IP and Port number and generated the Socket address for this Ip address.
Reference: https://nodejs.org/api/net.html#class-netsocketaddress
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read