I am working on a machine with Arduino motors for rotation on yaw and pitch axis. Also I have a nodejs server implementation to send data to machine (list of coordinates). For this purpose I have implemented a TCPClient class in TCP_Client.js as following:
var net = require('net');
/***********************************************************************************************************************
---------------------------------------------------Client TCP socket----------------------------------------------------
* *********************************************************************************************************************/
const port = 5303;
const host = 'localhost';
// creating a custom socket client and connecting it....
var client;
class TCPClient {
constructor(host, port) {
this.host = host;
this.port = port;
client = new net.Socket();
client.connect({
port: this.port,
host: this.host
});
this.onConnect();
}
onConnect() {
client.on('connect',function(){
console.log('Client: connection established with server');
console.log('---------client details -----------------');
var address = client.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Client is listening at port' + port);
console.log('Client ip :' + ipaddr);
console.log('Client is IP4/IP6 : ' + family);
});
client.on('drain', function(){
console.log("DRAIN");
});
this.setEncoding();
client.on('data', function(data) {
console.log("RECEIVED");
client.end();
});
client.on('end', function(){
console.log("END");
});
}
setEncoding() {
console.log("setEncoding");
client.setEncoding('utf8');
}
onData(data) {
console.log("DATA= ", data);
client.write(data);
}
end() {
client.end();
}
close() {
client.close();
}
}
//CREATE A TCP CLIENT OBJECT
exports.newTCPclient = function (host, port) {
console.log("CONSTRUCT TCP CLIENT");
var tc = new TCPClient(host, port);
return tc;
}
In server.js I initialize a global TCPClient as follows:
const port = 5033;
const host = '192.168.100.20';
var tcp_client = tcpclient.newTCPclient(host, port);
I send the coordinates merged in a text variable named coords as follows:
tcp_client.onData(coords);
Everytime I move the arduino motors I repeat this but after a short while I encounter this error:
events.js:174
throw er; //Unhandled 'error' event
Error: read ECONNRESET
at TCP.onStreamRead(internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process. tckCallback (internal/process/next_tick.js:63:19)
I am not very fluent with connections. Is it related to the TCP client class? If so how can I fix it?
Thank you for all the help.
ECONNRESET means the other side of the TCP conversation abruptly closed its end of the connection. Make sure it's closing the connection properly at the receiving end.
use --abort-on-uncaught-exception node option when running code, it will show you verbose report
Related
My plan is to create a proxy of my phone 4g connection.
I've made a direct tcp connection of my phone to my pc via nodejs.
I create a client.js on my phone and server.js on my pc. They connect.
Now i need to 'transform' this direct connection into a proxy, but i dont know how.
Any help i would aprecciate.
I will show my server.js and client.js code below.
Server.js
var net = require('net');
var tcpServerPort = 7000;
// a simple TCP server for testing
var server = net.createServer(function (socket) {
console.log('Client connected to server');
socket.on('close', function () {
console.log('Client disconnected from server');
});
socket.on('data', function (buffer) {
// 'echo' server
socket.write(buffer);
});
socket.on('error', function (err) {
console.log('Error: ' + err.soString());
});
});
server.listen(tcpServerPort);
Client.js
const net = require('net');
const client = new net.Socket();
const port = 7000;
const host = 'my home ip';
client.connect(port, host, function() {
console.log('Connected');
client.write("Hello From Client " + client.address().address);
});
client.on('data', function(data) {
console.log('Server Says : ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});
I am developing socket application in my server with node js. I am just listening 9000 port. I am checking the data that client sent to this tcp port, if a client made http request, I kick client from server. Because some bots in the internet does that and I dont want them in my system. Due to test purposes, I try to connect that port with a browser, hold down F5 refresh button continuously, then application crashes immediately. I am simulating the DDOS attacks in my port by this way. The error message as follows:
events.js:183
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at _errnoException (util.js:992:11)
at TCP.onread (net.js:618:25)
And here is my basic TCP listener code
var net = require("net");
var server = net.createServer();
server.on("error", err =>
{
console.log("error handled, error is : %s",err);
});
server.on("connection",function(socket)
{
var remoteAddress = socket.remoteAddress;
console.log("new client connection %s",remoteAddress);
socket.end();
});
server.listen(9000, function()
{
console.log("I am listening.");
});
What can be done to save TCP port from HTTP connections and internet bots?
Put this under socket.end():
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
full code:
var net = require("net");
var server = net.createServer();
server.on("error", err =>
{
console.log("error handled, error is : %s",err);
});
server.on("connection",function(socket)
{
var remoteAddress = socket.remoteAddress;
console.log("new client connection %s",remoteAddress);
socket.end();
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
});
server.listen(9000, function()
{
console.log("I am listening.");
});
I’m trying to create a Node js TCP server and client. Currently I’m running the server on the IP address of 127.0.0.1:8080.
And my client is trying to connect to that IP. I have no idea as to why this is failing, Here is my code:
var net = require('net');
const readline = require('readline');
var clients = [];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function OpenServer() {
var server = net.createServer(function(socket) {
socket.write('Echo server\n');
socket.name = socket.remoteAddress + ":" + socket.remotePort;
socket.write(socket.name + " joined the chat\n");
socket.on('data', function(data) {
socket.write('socket Received: ' + data + '\n');
});
socket.on('connect', function(data){
socket.write("new connection");
});
socket.pipe(socket);
}).listen(8080, '127.0.0.1');
}
function OpenClient() {
var client = new net.Socket();
client.connect(8080, "127.0.0.1", function() {
console.log('Connected');
});
client.on('data', function(data) {
console.log('Client Received: ' + data + '\n');
rl.question('How are you today? ', (answer) => {
// TODO: Log the answer in a database
client.write(`Thank you ${answer}`);
rl.close();
});
});
client.on('close', function() {
console.log('Connection closed');
});
}
OpenClient(); OpenServer();
Here is the error I get when i run the client.
Process crashed with: Error: connect ECONNREFUSED 127.0.0.1:8080
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1174:14)
I’d also like to say, I kind of don’t really know what the IP 127.0.0.1 is.
If anyone has a better solution or better way of doing this, I’m very open to here it.
I'm trying to use named pipes in my application. The problem is when I try to connect to the named pipe before the server is running, I get the following error:
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ENOENT \\?\pipe\\testpipe
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1062:14)
How can I check if the pipe exists before attempting to connect to it?
Note: Wrapping my connect code in a try-catch doesn't prevent the error.
Here is my code:
var net = require('net');
var addr = '\\\\?\\pipe\\testpipe';
var client = net.createConnection({ path: addr }, function() {
console.log("Connected");
client.on('data', function(data) {
console.log("Recieved: " + data);
});
client.on('error', function(){
console.log(arguments);
});
}.bind(this));
Using the domain module prevents a fatal error. The following code can be used to safely run the connect code.
Not what I was hoping for, but the closed solution since there have been no answers.
var net = require('net');
var domain = require('domain');
var addr = '\\\\?\\pipe\\testpipe';
var d = domain.create();
d.on('error', function(err) {
console.error(err);
});
d.run(function() {
var client = net.createConnection({ path: addr }, function() {
console.log("Connected");
client.on('data', function(data) {
console.log("Recieved: " + data);
});
client.on('error', function(){
console.log(arguments);
});
}.bind(this));
});
make the socket, then listen for an error event, then call connect and it won't be thrown: https://nodejs.org/api/net.html#net_event_error_1
I'm trying to punch a TCP hole through two NATs in node.js. The problem is I can't figure out how to choose which local port the connection should use?
After creating a connection with the public server, you also need to listen on the exact same local (!!) port that was used to establish that connection.
I extended your testcode to a complete tcp hole punching proof of concept:
// server.js
var server = require('net').createServer(function (socket) {
console.log('> Connect to this public endpoint with clientB:', socket.remoteAddress + ':' + socket.remotePort);
}).listen(4434, function (err) {
if(err) return console.log(err);
console.log('> (server) listening on:', server.address().address + ':' + server.address().port)
});
// clientA.js
var c = require('net').createConnection({host : 'PUBLIC_IP_OF_SERVER', port : 4434}, function () {
console.log('> connected to public server via local endpoint:', c.localAddress + ':' + c.localPort);
// do not end the connection, keep it open to the public server
// and start a tcp server listening on the ip/port used to connected to server.js
var server = require('net').createServer(function (socket) {
console.log('> (clientA) someone connected, it\s:', socket.remoteAddress, socket.remotePort);
socket.write("Hello there NAT traversal man, this is a message from a client behind a NAT!");
}).listen(c.localPort, c.localAddress, function (err) {
if(err) return console.log(err);
console.log('> (clientA) listening on:', c.localAddress + ':' + c.localPort);
});
});
// clientB.js
// read the server's output to find the public endpoint of A:
var c = require('net').createConnection({host : 'PUBLIC_IP_OF_CLIENT_A', port : PUBLIC_PORT_OF_CLIENT_A},function () {
console.log('> (clientB) connected to clientA!');
c.on('data', function (data) {
console.log(data.toString());
});
});
For a more complete version with signalling happening on the server, I refer to my code here: https://github.com/SamDecrock/node-tcp-hole-punching
The socket is assigned a local port. To reuse the same port you can connect to the client using the same socket that was used to communicate with the server. This works for you because you are doing TCP hole punching. However, you cannot choose a port yourself.
Here is some test code:
// server.js
require('net').createServer(function(c) {
c.write(c.remotePort.toString(10));
}).listen(4434);
//client.js
var c = require('net').createConnection({host : '127.0.0.1', port : 4434});
c.on('data', function(data) {
console.log(data.toString(), c.localPort);
c.end();
});
c.on('end', function() {
c.connect({host : '127.0.0.1', port : 4434});
});