2ND STEP TO CREATE A TCP CLIENT ON A TOUCHBERRY PI 3. CREATING A TCP CLIENT USING NODE.JS

2ND STEP TO CREATE A TCP CLIENT ON A TOUCHBERRY PI 3. CREATING A TCP CLIENT USING NODE.JS

On this post it showed how to implement a TCP server on Touchberry Pi 3

The requirements of this post is just a Touchberry Pi with internet connection.

To get the Node.js JavaScript interpreter we need to install the command cURL on our Linux.

cURL command is  very useful for HTTP/S requests. To get the cURL we just need to type on the Linux Terminal:

sudo apt-get install curl

 

Once there is the cURL installed we can download the node source typing on the terminal: 

curl -sL https://deb.nodesource.com/setup_6.x > setup_node.sh

 

With this cURL command we save what is on the link with the name of “setup_node.sh”. Next we have to change the permissions of setup_node.sh typing on the terminal: 

chmod 755 setup_node.sh

 

chmod modifies the permission of the file. With the next line we will execute the file setup_node.sh as a root, this command can take a while.

sudo ./setup_node.sh  

 

After that we are available to install the node.js typing, this command can time several minutes as well:

sudo apt-get install nodejs

 

Next we create a directory to save the Node.js JavaScript server file typing on the terminal: 

mkdir tcpClientExample

 

Inside of this directory is where on this example has been chosen to save the server.js file. 

Next it is showed the client.js file, there are some comments to get a better understanding of what is going on on every code line:

var net = require('net');

var client = new net.Socket();
 client.connect(1337, '127.0.0.1', function() { // configuration of the connection
       console.log('Connected');
       client.write('Hello, server! Love, Client.');
 });

client.on('data', function(data) {            // when data arrives to the client execute the next line
       console.log('Received: ' + data);
       client.destroy(); // kill client after server's response
 });

client.on('close', function() {               // when client close the connection execute the next line
       console.log('Connection closed');
 });

After save the server file on tcpServerExample directory follow the next instructions to execute the TCP server. Fist just go to the tcpServerExample directory typing  on the terminal:

cd tcpServerExample

 

Now all is ready to execute the server.js typing on terminal:

node client.js

 

See also:

HOW TO USE AN RTC (REAL TIME CLOCK) ON TOUCHBERRY B3 PI,

HOW TO CREATE USD LINUX TO TOUCHBERRY PI3 AND HUMMTOUCH,

SIMPLECOMM LIBRARY TO SEND DATA THROUGH ANY ARDUINO STREAM, 

HOW TO INSTALL SCREENSAVER ON TOUCHBERRY PI

Los comentarios están cerrados