I have a telnet server embedded in a C++ app that I can connect to with no problem
using telnet.
I want to write a node application that will connect to my server and I have tried
this
var net = require('net');
var port = 6502
var host = '127.0.0.1'
var socket = net.connect(port,host, function() {
console.log("Sending data");
socket.write("hello\r\n")
socket.on("data", function (data) {
console.log("received data");
console.log( data.toString() );
socket.end();
})
})
socket.on("error", function(err) {
console.log("Error");
console.log(err);
})
Unfortunately what I get back is this
> node test.js
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
What's really odd is if I set up a simple echo server with node everything works
fine. Here's the working echo server code:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(6502, '127.0.0.1');
and from that I get
Sending data
received data
Echo server
hello
Is there any reason why:
I can telnet into my app fine
I can connect from node to my node echo server on the same port
I get a connection refused if I connect from my node app to my app
Extra Info
On OSX (mavericks)
Node version 0.10.28
Telnet server in C++ is provided via embedded lua and luasocket (lua 5.1)
Solved!
The issue is the code in my app server was binding to localhost and by default
that binds to the IPV6 address of ::1
Passing a host of localhost to net.connect assumes IPV4 and doesn't work.
The mac command line telnet and nc both work fine with this and connect correctly.
Two solutions:
App binds to 127.0.0.1 and localhost in node works fine
Set host address to ::1 in test.js and it connects via ipv6
All fixed now though :)
gaz
Looks like you forgot to tell the server to listen in your code. It throws a connection refused error because there is nothing to connect to...
Add this at the end: server.listen(port);
The prototype for net.connect is (options, callback)
See http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener
I would then suggest to test your code against a standard telnet server to see how it behaves, and finally I would strongly recommend the use of jshint or jslint.
Related
I have a redis server which is working fine in my local but in ubuntu server is not working can someone gives the comment for installing redis in server
it is not working even with docker it is working only while i am running in local
const redis=require('redis');
var redisClient:any;
(async () => {
try {
redisClient = redis.createClient({ socket: { port: 6379 } });
await redisClient.connect();
// const redisClient = redis.createClient({
// port:"6379",
// host:'redis-service'
// });
redisClient.on('connect',()=>{
console.log('server connected to redis')
})
redisClient.on('ready',()=>{
console.log('Client Connect to redis and ready to use')
})
redisClient.on('error',(err:any)=>{
console.log(err)
})
redisClient.on('end',()=>{
console.log('Server disconnected from redis')
})
process.on('SIGINT',()=>{
redisClient.quit()
})
console.log('connected');
} catch (err) {
console.error(err)
}
})()
export{
redisClient
};
Make sure that Redis is not already running on the server. You can check this by running the command ps aux | grep redis. If Redis is running, you should see a line with the redis-server process.
Confirm that Redis is properly installed on your server by running redis-cli ping. If Redis is installed and running, it should return "PONG"
Check the Redis configuration file (redis.conf) and ensure that the IP and port settings match the settings on your local machine.
Make sure that the Redis server has the necessary permissions to access its data directory and that the correct ownership and permissions are set on its files.
Verify that the firewall rules on the server allow incoming connections on the Redis port (usually 6379)
Check for any compatibility issues between the version of Redis that you're running on your local machine and the version of Redis that's running on the Ubuntu server. If there's a version mismatch, it may be necessary to upgrade or downgrade one of the installations.
Try to run the Redis server with verbose output by running the command redis-server -v, this can give you some more information about the errors that are causing the server to fail.
Look at Redis log files, usually located in /var/log/redis for further clues about the cause of the problem.
const redis = require('redis');
require('dotenv').config();
console.log(process.env.redisHost, ':', process.env.redisPort);
const redisClient = redis.createClient({
host: process.env.redisHost,
port: process.env.redisPort,
password: process.env.redisKey
});
redisClient.connect();
redisClient.on('error', err => console.log('Redis error: ', err.message));
redisClient.on('connect', () => console.log('Connected to redis server'));
module.exports = redisClient;
I tried this sample from redis docs but still I'm getting an error stating:
Redis error: connect ECONNREFUSED 127.0.0.1:6379
I logged the environment host and port variables to the console and I got the remote host ipv4 address, but still the client is trying to connect to localhost instead of remote host (I purposely uninstalled redis from my local device to check if the client is working as it is supposed to). I also confirmed that the remote redis host is working perfectly.
I also tried different methods like :
https://cloud.google.com/community/tutorials/nodejs-redis-on-appengine
redis.createClient(port, host, {auth_pass: password});
But still, I got the same error.
I am able to connect to the redis host via commandline:
redis-cli.exe -h XX.XX.XX.XXX -a Password
XX.XX.XX.XXX:6379> set name dhruv
OK
XX.XX.XX.XXX:6379> get name
"dhruv"
XX.XX.XX.XXX:6379> exit
I'm trying to use redis on nodejs for the first time, so don't have a proper idea but I think I am doing everything right.
Any solution/workaround will be helpful :D
It worked with this code:
const url = `redis://${process.env.redisHost}:${process.env.redisPort}`;
const redisClient = redis.createClient({
url,
password: process.env.redisKey
});
redisClient.connect();
can you check if in the destination the port is reachable. it maybe the firewall block your access
So I have a web service running on port 7001 over TCP on:
net.tcp://www.myurl.com:7001/my/webservice
and I want to connect to it using net.Socket:
client.connect(7001, 'myurl.mine.com/my/webservice', function () {
console.log('Connected');
client.write(msg);
});
When i do the above, it gives an exception:
Error: getaddrinfo ENOTFOUND <<my url>>
at GetAddrInfoReqWrap.onLookup [as oncomplete]
when i try connect to it without the /my/webservice, it connects fine and doesnt give an error at the client.connect() stage, but obviously cant find that endpoint so it gives another error when i try to do the client.write()
Any one any idea how to use net.Socket against a web service with a url that isnt just myurl.com:7001 but actually contains a route like myurl.com:7001/my/webservice?
According to node.js docs the version of the method you are using expects only a hostname for a second argument. You are providing hostname + path
In your case I would recommend using a more generalized version of net.connect which accepts an object with parameters. So your code will look something like this:
client.connect({
host: 'myurl.mine.com',
port: 7001,
path: '/my/webservice'
},
function () {
console.log('Connected');
client.write(msg);
}
);
I am trying to create a Socket server, but I keep getting ECONNREFUSED when I try and launch the server.
var net = require('net');
var server = net.createConnection(3000,'localhost',function(socket) {
console.log('connected!');
});
server.on('error', function(e) {
console.log(e);
});
Nothing fancy.
However when I try to run it node server.js, I only get an error message. Is there a step I am missing here? Is it maybe a configuration issue?
I am running node 0.10.32 if that helps.
Thanks
That error is your client cant connect to server. You can add this code to snip error.
server.on('error', function(err){
console.log(err.message);
});
Did your server start ? You can use http.createServer to create server listen on port 3000 and you connect to it.
I created a very simple Node.JS script which is suppose to connect to a server on port 3200. The code works great on Windows 8.1 x64, but when I moved it to a Ubuntu Server machine I am getting a ECONNREFUSED.
I tried to connect via IP Address and the Domain Name, but neither work. It always gives me ECONNREFUSED.
When I SSH into the Ubuntu Server I can do a telnet to the server/port and it works fine. Also pinging the server works. Just Node.JS doesnt connect.
Server:
Ubuntu Server 12.04.3 LTS (precise)
Here is the code:
var net = require('net');
var socket = net.connect({port: 3200, host: '172.19.16.203'}, function() {
// Connection Successful
console.log('Socket connected...');
// Data Received from Server
socket.on('data', function(d) {
console.log('data');
});
socket.on('end', function() {
});
socket.on('error', function(e) {
});
});
I found the solution. I was running my script by the following command:
node test.js
I guess the node is an old version. I upgraded the OS to the latest version and then did:
apt-get install nodejs
I run the script like following:
nodejs test.js
And it works!