Sajjad
Sajjad

Reputation: 121

error while sending data to tcp port using tcp tunneling using ngrok

I want to sent a TCP data to a port which was exposed globally. I have written a server code:

server.js

const net = require('net');
const port = 8080;
const host = '127.0.0.1';
var fs = require("fs");

const server = net.createServer();
server.listen(port, host, () => {
    console.log('TCP Server is running on port ' + port + '.');
});

let sockets = [];

fs.writeFile('tcpdata.log', 'id,pm25,pm10,temp,hum,atm,loc,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10.m11,m12,m13,m14,m15,m16\n', function (err) {
  if (err) throw err;
  console.log('Created successfully.');
});

server.on('connection', function(sock) {
    console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);

    sockets.push(sock);

    sock.on('data', function(data) {
        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        fs.appendFile('tcpdata.log', data, function(error){console.log('data written');});
        // Write the data back to all the connected, the client will receive it as data from the server
        sockets.forEach(function(sock, index, array) {
            sock.write(sock.remoteAddress + ':' + sock.remotePort + " said " + data + '\n');
        });
    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        let index = sockets.findIndex(function(o) {
            return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
        })
        if (index !== -1) sockets.splice(index, 1);
        console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
    });
});

Steps followed to send data to TCP port.

  1. started the server.js

server.js

  1. Connect to tcp port 8080

tcp port connecting to 8080

  1. AT Commands to send data to tcp port "2.tcp.ngrok.io:16185"

    AT+CREG=1

    OK

    AT+CREG?

    +CREG: 1

    AT+GPS=1

    OK

    AT+CGATT=1

    +CGATT:1

    OK

    AT+CGDCONT=1,"IP","pinternet.interkom.de"

    OK

    AT+CGACT=1,1

    OK

    AT+CIPSTART="TCP","2.tcp.ngrok.io",16185

    CONNECT OK

    OK

    AT+CIPSEND=101,"'A1',22.00,31.00,7.00,15.00,0.00,$GNGGA,170219.094,4900.5489,N,00825.5612,E,0,3,,311.5,M,47.9,M,,*51"

    OK

    AT+CIPSTART="TCP","2.tcp.ngrok.io",16185

    ALREAY CONNECT

    OK

    AT+CIPSEND=101,"'A1',23.00,33.00,6.00,14.00,1.00,$GNGGA,170230.000,4900.5372,N,00825.4908,E,0,4,,227.4,M,47.9,M,,*53"

    OK

  2. Usually the data is sent without errors, but there some unkown trash data appearing on tcp port. And there are some close of ports. Can I fixed this unexpected close ups and trash data apearing on the tcp port. Is my code right?

  3. Usuall data reception: data received

  4. Unexpected Errors and Closure on TCP ports

events.js:353
      throw er; // Unhandled 'error' event

or

data written
CONNECTED: 127.0.0.1:58072
DATA 127.0.0.1: SSH-2.0-Renci.SshNet.SshClient.0.0.1

or

another error

Can you please help getting this fixed. How can I avoid un-expected tcp connection closure or avoid trash data.

Upvotes: 0

Views: 609

Answers (1)

Ron Klein
Ron Klein

Reputation: 9460

  1. I think you should also listen to error event
  2. You assume that the on('data') handler, on a given event, handles exactly one command/line? If so, then that's not always the case: you might get partial data, or more than a single command. If newline character (\n) is your delimiter, then you should use a local variable to buffer the input until you get this delimiter and then treat the buffered data as a single line.

Upvotes: 1

Related Questions