0% found this document useful (0 votes)
14 views7 pages

Node Console

Database Management Systems (DBMS) are essential to the telecommunications industry because they manage enormous volumes of data on billing, customer information, and network optimization.

Uploaded by

hemalatha.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Node Console

Database Management Systems (DBMS) are essential to the telecommunications industry because they manage enormous volumes of data on billing, customer information, and network optimization.

Uploaded by

hemalatha.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

node.

js console is a global object and is used to print different


levels of messages to stdout and stderr. There are built-in
methods to be used for printing informational, warning, and error
messages.

It is used in synchronous way when the destination is a file or a


terminal and in asynchronous way when the destination is a pipe.

Console Methods

Following is a list of methods available with the console global


object.

Sr.N
Method & Description
o.

[Link]([data][, ...])
1 Prints to stdout with newline. This function can take multiple
arguments in a printf()-like way.

[Link]([data][, ...])
2 Prints to stdout with newline. This function can take multiple
arguments in a printf()-like way.

[Link]([data][, ...])
3 Prints to stderr with newline. This function can take multiple
arguments in a printf()-like way.

[Link]([data][, ...])
4 Prints to stderr with newline. This function can take multiple
arguments in a printf()-like way

5 [Link](obj[, options])
Uses [Link] on obj and prints resulting string to stdout.

6 [Link](label)
Mark a time.

7 [Link](label)
Finish timer, record output.

[Link](message[, ...])
8 Print to stderr 'Trace :', followed by the formatted message and
stack trace to the current position.

[Link](value[, message][, ...])


9 Similar to [Link](), but the error message is formatted as
[Link](message...).
Example
Let us create a js file named [Link] with the following code −
Live Demo
[Link]("Program Started");

var counter = 10;


[Link]("Counter: %d", counter);

[Link]("Getting data");
//
// Do some processing here...
//
[Link]('Getting data');

[Link]("Program Ended")

Now run the [Link] to see the result −

node [Link]

Verify the Output.

Program Started
Counter: 10
Getting data: 0ms
Program Ended

Use Sockets to send and receive data over TCP


 Article
 12/01/2022
 2 contributors
Feedback

In this article
1. Create an IP endpoint
2. Create a Socket client
3. Create a Socket server
4. Run the sample client and server
5. See also
Before you can use a socket to communicate with remote devices, the
socket must be initialized with protocol and network address information.
The constructor for the Socket class has parameters that specify the
address family, socket type, and protocol type that the socket uses to
make connections. When connecting a client socket to a server socket,
the client will use an IPEndPoint object to specify the network address of
the server.

Create an IP endpoint

When working with [Link], you represent a network


endpoint as an IPEndPoint object. The IPEndPoint is constructed with
an IPAddress and its corresponding port number. Before you can initiate a
conversation through a Socket, you create a data pipe between your app
and the remote destination.

TCP/IP uses a network address and a service port number to uniquely


identify a service. The network address identifies a specific network
destination; the port number identifies the specific service on that device
to connect to. The combination of network address and service port is
called an endpoint, which is represented in the .NET by the EndPoint class.
A descendant of EndPoint is defined for each supported address family; for
the IP address family, the class is IPEndPoint.

The Dns class provides domain-name services to apps that use TCP/IP
internet services. The GetHostEntryAsync method queries a DNS server to
map a user-friendly domain name (such as "[Link]") to a
numeric Internet address (such as [Link]). GetHostEntryAsync returns
a Task<IPHostEntry> that when awaited contains a list of addresses and
aliases for the requested name. In most cases, you can use the first
address returned in the AddressList array. The following code gets
an IPAddress containing the IP address for the server [Link].

C#Copy
IPHostEntry ipHostInfo = await [Link]("[Link]");
IPAddress ipAddress = [Link][0];
Tip

For manual testing and debugging purposes, you can typically use
the GetHostEntryAsync method to get given
the [Link]() value to resolve the localhost name to an IP
address.

The Internet Assigned Numbers Authority (IANA) defines port numbers for
common services. For more information, see IANA: Service Name and
Transport Protocol Port Number Registry). Other services can have
registered port numbers in the range 1,024 to 65,535. The following code
combines the IP address for [Link] with a port number to create
a remote endpoint for a connection.

C#Copy
IPEndPoint ipEndPoint = new(ipAddress, 11_000);

After determining the address of the remote device and choosing a port to
use for the connection, the app can establish a connection with the
remote device.

Create a Socket client


With the endPoint object created, create a client socket to connect to the
server. Once the socket is connected, it can send and receive data from
the server socket connection.

C#Copy
using Socket client = new(
[Link],
[Link],
[Link]);

await [Link](ipEndPoint);
while (true)
{
// Send message.
var message = "Hi friends 👋!<|EOM|>";
var messageBytes = [Link](message);
_ = await [Link](messageBytes, [Link]);
[Link]($"Socket client sent message: \"{message}\"");

// Receive ack.
var buffer = new byte[1_024];
var received = await [Link](buffer, [Link]);
var response = [Link](buffer, 0, received);
if (response == "<|ACK|>")
{
[Link](
$"Socket client received acknowledgment: \"{response}\"");
break;
}
// Sample output:
// Socket client sent message: "Hi friends 👋!<|EOM|>"
// Socket client received acknowledgment: "<|ACK|>"
}

[Link]([Link]);

The preceding C# code:

 Instantiates a new Socket object with a given endPoint instances


address family, the [Link], and [Link].
 Calls the [Link] method with
the endPoint instance as an argument.

 In a while loop:

o Encodes and sends a message to the server


using [Link].
o Writes the sent message to the console.
o Initializes a buffer to receive data from the server
using [Link].
o When the response is an acknowledgment, it is written to
the console and the loop is exited.

 Finally, the client socket


calls [Link] given [Link], which shuts
down both send and receive operations.

Create a Socket server


To create the server socket, the endPoint object can listen for incoming
connections on any IP address but the port number must be specified.
Once the socket is created, the server can accept incoming connections
and communicate with clients.

C#Copy
using Socket listener = new(
[Link],
[Link],
[Link]);

[Link](ipEndPoint);
[Link](100);

var handler = await [Link]();


while (true)
{
// Receive message.
var buffer = new byte[1_024];
var received = await [Link](buffer, [Link]);
var response = [Link](buffer, 0, received);

var eom = "<|EOM|>";


if ([Link](eom) > -1 /* is end of message */)
{
[Link](
$"Socket server received message: \"{[Link](eom,
"")}\"");

var ackMessage = "<|ACK|>";


var echoBytes = [Link](ackMessage);
await [Link](echoBytes, 0);
[Link](
$"Socket server sent acknowledgment: \"{ackMessage}\"");
break;
}
// Sample output:
// Socket server received message: "Hi friends 👋!"
// Socket server sent acknowledgment: "<|ACK|>"
}

The preceding C# code:

 Instantiates a new Socket object with a given endPoint instances


address family, the [Link], and [Link].

 The listener calls the [Link] method with


the endPoint instance as an argument to associate the socket
with the network address.

 The [Link]() method is called to listen for incoming


connections.

 The listener calls the [Link] method to accept an


incoming connection on the handler socket.

 In a while loop:

o Calls [Link] to receive data from the client.


o When the data is received, it's decoded and written to the
console.
o If the response message ends with <|EOM|>, an
acknowledgment is sent to the client using
the [Link].

Run the sample client and server

Start the server application first, and then start the client application.

.NET CLICopy
dotnet run --project socket-server
Socket server starting...
Found: [Link] available on port 9000.
Socket server received message: "Hi friends 👋!"
Socket server sent acknowledgment: "<|ACK|>"
Press ENTER to continue...

The client application will send a message to the server, and the server
will respond with an acknowledgment.

.NET CLICopy
dotnet run --project socket-client
Socket client starting...
Found: [Link] available on port 9000.
Socket client sent message: "Hi friends 👋!<|EOM|>"
Socket client received acknowledgment: "<|ACK|>"
Press ENTER to continue...

You might also like