Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs tcp client

// Include Nodejs' net module.
const Net = require('net');
// The port number and hostname of the server.
const port = 8080;
const host = 'localhost';

// Create a new TCP client.
const client = new Net.Socket();
// Send a connection request to the server.
client.connect({ port: port, host: host }), function() {
    // If there is no error, the server has accepted the request and created a new 
    // socket dedicated to us.
    console.log('TCP connection established with the server.');

    // The client can now send data to the server by writing to its socket.
    client.write('Hello, server.');
});

// The client can also receive data from the server by reading from its socket.
client.on('data', function(chunk) {
    console.log(`Data received from the server: ${chunk.toString()}.`);
    
    // Request an end to the connection after the data has been received.
    client.end();
});

client.on('end', function() {
    console.log('Requested an end to the TCP connection');
});
Comment

tcp listen node

var net = require('net');

var server = net.createServer();    
server.on('connection', handleConnection);
server.listen(9000, function() {    
  console.log('server listening to %j', server.address());  
});
function handleConnection(conn) {    
  var remoteAddress = conn.remoteAddress + ':' + conn.remotePort;  
  console.log('new client connection from %s', remoteAddress);
  conn.on('data', onConnData);  
  conn.once('close', onConnClose);  
  conn.on('error', onConnError);
  function onConnData(d) {  
    console.log('connection data from %s: %j', remoteAddress, d);  
    conn.write(d);  
  }
  function onConnClose() {  
    console.log('connection from %s closed', remoteAddress);  
  }
  function onConnError(err) {  
    console.log('Connection %s error: %s', remoteAddress, err.message);  
  }  
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: regex for no whitespace at the beginning and end 
Javascript :: check if element is array javascript 
Javascript :: ace get contents of editor 
Javascript :: get element size javascript 
Javascript :: JS node instal fs 
Javascript :: moment js date diff 
Javascript :: Function in JavaScript that can be called only once 
Javascript :: newtonsoft json object to json string 
Javascript :: how to check chrome version in js 
Javascript :: mongoose count documents 
Javascript :: p5.js style 
Javascript :: Autocomplete height adjust in materil ui 
Javascript :: how to can i get custom data attribute value in javascript 
Javascript :: add expiry to jwt extended token 
Javascript :: MongoParseError: option usecreateindex is not supported 
Javascript :: getelementbytagname javascript 
Javascript :: console.log color terminal 
Javascript :: most 5 spoken language in countries array in js 
Javascript :: like in javascript 
Javascript :: angular how to get previous state 
Javascript :: convert base64 to uint8array javascript 
Javascript :: express post body 
Javascript :: javascript division get remainder 
Javascript :: format number to 2 digits javascript 
Javascript :: javascript check if is array 
Javascript :: data-widget="pushmenu" collable automatically 
Javascript :: javascript push in specific index 
Javascript :: check when keyup an input from a specific form jquery 
Javascript :: javascript loop through objec 
Javascript :: jquery change value 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =