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 :: check if string contains lowercase javascript 
Javascript :: javascript cors error 
Javascript :: Javascript form check to see if return or enter was pressed 
Javascript :: correct json type 
Javascript :: reactjs cdn 
Javascript :: angular get current route 
Javascript :: js tab character 
Javascript :: javascript break foreach 
Javascript :: javascript element edit text 
Javascript :: jquery wait for all ajax requests to complete 
Javascript :: javascript pass parameter to event listener 
Javascript :: add parameter to url without reload jquery 
Javascript :: map javascript index 
Javascript :: javascript easiest way to get second parent 
Javascript :: axios get data 
Javascript :: absolute value array javascript 
Javascript :: print json shopify 
Javascript :: basic express graphql 
Javascript :: js to lowercase 
Javascript :: keep-alive vuejs router 
Javascript :: onclick function jquery 
Javascript :: jquery hover event 
Javascript :: how to check localstorage not set 
Javascript :: document.on chenage jquer 
Javascript :: check if object has key lodash 
Javascript :: ngstyle background url angular 
Javascript :: setinterval stop onditional stop 
Javascript :: javascript urlsearchparams to object 
Javascript :: javascript destructure object and rename 
Javascript :: how to comment in a json file 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =