The Wayback Machine - https://web.archive.org/web/20240626080859/https://www.geeksforgeeks.org/node-js-net-socketaddress-method/
Open In App

Node.js net.SocketAddress() Method

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Difference Between Node.js and Asp.net
ASP.NET: It is an open-source web application framework initially discharged by Microsoft in January 2002 with the primary cycle of the .NET system. It is built on the Common Dialect Runtime (CLR), permitting the utilization of any .NET dialect counting C# (object-oriented), F# (utilitarian to begin with), VB .NET (a bequest of Visual Essential, ut
4 min read
Explain ConfigureServices and Configure method in ASP.NET ?
In this article, we will see what is Configure and ConfigureService methods. Basically, these two methods play a very important role in startup class. The baseline of this class is to reduce the dependency of the application on the server. Actually, The Startup class is required to define two methods that will be invoked automatically by ASP.NET Co
8 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 &lt;= n. Examples: Input : list: 1-&gt;3-&gt;4-&gt;5 n = 4, x = 2 Output : 1-&gt;2-&gt;3-&gt;4-&gt;5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10-&gt;8
5 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic
2 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
Encountering the error message ‘node’ is not recognized as an internal or external command after installing Node.js can be frustrating, especially when you’re eager to start working on your project. This error usually means that your system cannot find the Node.js executable because the path to it is not set correctly in your system's environment v
2 min read
Node.js Http2ServerRequest.method Method
The Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the string represe
3 min read
Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string. Syntax: request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request type name as a
2 min read
three90RightbarBannerImg