events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:4562
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)
here is my code
var express = require('express');
var net = require('net');
var app = express();
var i = 0;
app.get('/', function (req, res)
{
i = i+1; //no of clinets request gin ne ki liye
console.log(i + "fa..sa");
var client = net.connect({port: 4562},"192.168.202.101", function() {
console.log('connected to server!'); });
client.write("Ho gaya bhai");
client.end();
res.send('ho gaya bhai..');
});
app.listen(6544);
As you can see from your error message you have an error in the way you call net.connect. It looks like you want to connect to 192.168.202.101:4562, but you try to connect to ´127.0.0.1:4562´. Try either:
net.connect({ port: 4562, host: "192.168.202.101"}, function(){});
or:
net.connect(4562, "192.168.202.101", function() {});
Related
I wanna make an tcp server with nodejs . When i start the server in the terminal it says nothing, and when i start the client the client gets the message but in the server terminal, it prints:
events.js:292
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
Emitted 'error' event on Socket instance at:
at Socket.onerror (internal/streams/readable.js:760:14)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -54,
code: 'ECONNRESET',
syscall: 'read'
}
When i start the client again, the server is still running, i needed to stop the server using killall node.
How can i fix this error.
Server:
var net = require('net');
var server = net.createServer(function(socket) {
socket.write('Connected to server !\r\n');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
Client:
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
I'm using socket io on my laravel project and I have a problem with the request on my disconnect event in the socket.js file.
I have debugged my code and the error is with the request library.
Socket.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(5000);
io.on('connection', function (socket) {
console.log("new client connected " + socket.id);
socket.on('disconnect', function() {
var socket_id = socket.id;
console.log('client disconnected :C '+socket_id);
var request = require('request');
var url_request = 'http://example.com/play-session/close?socket_id=' + socket_id;
request(url_request, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('session closed '+socket_id);
}
});
});
});
Error:
SyntaxError: Error parsing /var/www/example.com/public_html/node_modules/aws4/package.json: Unexpected token <
at Object.parse (native)
at readPackage (module.js:113:52)
at tryPackage (module.js:123:13)
at Function.Module._findPath (module.js:190:18)
at Function.Module._resolveFilename (module.js:336:25)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/example.com/public_html/node_modules/request/request.js:11:12)
at Module._compile (module.js:456:26)
I don't know what is happening. I appreciate all the help, thanks.
Updated, Fix issue complete:
I fix this issue with this: https://www.npmjs.com/package/request#unix-domain-sockets, ussing that example like this:
Socket.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
var request = require('request');
server.listen(5000);
io.on('connection', function (socket) {
console.log("new client connected " + socket.id);
socket.on('disconnect', function() {
var socket_id = socket.id;
console.log('client disconnected :C '+socket_id);
var url_request = 'http://example.com/play-session/close?socket_id='+socket_id;
request.get(url_request); // this line fix it
});
});
Thanks to all for your help.
You can fix this issue with this: https://www.npmjs.com/package/request#unix-domain-sockets, ussing that example like this:
request.get(url_request);
Regards
I'm getting this error and unable to fix this.
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
var http = require('http');
var options = {
host: 'localhost',
port: '8081',
path: '/index.htm'
};
var callback = function(response){
var body = '';
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
console.log(body);
});
}
var req = http.request(options, callback);
req.end();
I am trying to get nodejs client-server with tcp working. This is my code:
server.js containing the code for server
var net = require('net');
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
console.log("Started Server");
server.listen(1337, '127.0.0.1');
client.js containing the code for client
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
when I run in 2 separate terminals node server.js , node client.js I get this error:
Started Server
events.js:154
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:856:11)
at TCP.onread (net.js:546:26)
Process finished with exit code 1
But if i Combine both of the codes together in a single process it works fine. Anyone have any idea what the problem can be?
I'm looking at this page: http://nodejs.org/api/net.html#net_net_createconnection_options_connectionlistener
Running the code from the page:
var net = require('net');
var client = net.connect({port: 8124},
function() { //'connect' listener
console.log('client connected');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
and I'm getting the error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
shell returned 8
version stuff:
~ % node --version
v0.10.25
~ % uname -a
Linux human1 3.13.0-031300-generic #201401192235 SMP Mon Jan 20 03:36:48 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
I've tried a lot of different ports and I'm positive node isn't already running
Is the net server you're trying to connect to running?
I tried this and it works for me:
net.js
var net = require('net');
var server = net.createServer(function(client) {
console.log('connected');
});
server.listen(8124);
var client = net.connect({port: 8124}, function() {
console.log('client connected');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
Run:
$ node net.js
connected
client connected