Following is a net tcp client code
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello server');
});
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');
});
client.write('Hello server') is written inside the client.connect scope.
Is there a way to use it outside the client.connect scope.
I tried the following code but failed to send anything to server. However the return value of client.write returned true.
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
});
client.write('Hello server'); //Did not work
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');
});
Node js net module server code:
var net = require('net');
var server = net.createServer(function (connection) {
console.log('client connected');
connection.on('data', function (data) {
console.log('data from flash = ' + data);
var jsonData = {};
jsonData.message = "joined";
var d = JSON.stringify(jsonData);
connection.write(d);
});
connection.on('end', function () {
console.log('client disconnected');
});
// connection.pipe(connection);
});
server.listen(3055, function () {
console.log('server is listening');
});
Action script code
this.login_socket.connect(this.server_ip,3055);
this.login_socket.addEventListener(Event.CONNECT,this.login_socket_onConnection);
this.login_socket.addEventListener(DataEvent.DATA,this.login_onData);
this.login_socket.addEventListener(IOErrorEvent.IO_ERROR,this.login_socket_onIOErrorEvent);
this.login_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.login_socket_SecurityErrorEvent);
this.login_socket.addEventListener(Event.CLOSE,this.login_socket_closeErrorEvent);
Can anyone please tell me how to use xml socket with node js net module? I have tried everything but this doesn't work at all. I want to create a socket connection for a flash game to the server. I am using laravel as backend. If anyone knows how to create it with php tell me. Thank you.
var net = require('net');
var server = net.createServer(function (connection) {
console.log('client connected');
connection.setEncoding("utf8");
// on receive data from client
connection.on('data', function (data) {
console.log('data from flash = ' + data);
var d = JSON.stringify({
message: "joined"
}) + "\0";
connection.write(d);
});
// on connection end
connection.on('end', function () {
console.log('client disconnected');
});
// on error
connection.on("error", function (exception) {
console.log('an error occurred: ' + exception);
});
});
server.listen(3055, function () {
console.log('server is listening');
});
I change nodejs server code to above code and it works fine now.
I'm listening all the connections from a GPS device who is send the data to a our server. So I create a small NodeJs app to retrieve that information.
var net = require('net');
var server = net.createServer(function(connection) {
connection.on('drain', function(){
console.log('drain');
});
connection.on('data', function(data){
var dataGps = data.toString('ascii');
var fecha = new Date();
console.log(String(fecha));
var dataArray = dataGps.split(',');
});
connection.on('end', function() {
//console.log('Client disconnected'.magenta);
});
connection.on('error', function(err) {
//console.log("Connection error: " + err+" ... ".red);
//console.log('Closing connection....'.red);
//connection.destroy();
});
connection.on('close', function(){
console.log('closed event fired');
});
});
server.on('connection', function(){
console.log('new connection');
});
server.getConnections(function(err, count){
if (err) {
console.log('# Error: '+err);
}
console.log('Count: '+count);
});
server.listen(3001, function() {
console.log('Server is listening on port 3001...');
});
To keep running this App, I'm using pm2 (pm2 website).
Everything is fine except that sometimes this app becomes inactive and don't do anything until I restart it with pm2.
This app is in Amazon EC2 (t2.medium instance) Ubuntu 16.04.
I don't know what I'm doing wrong. Any help would be really helpful.
I created a simple tcp server using code
var net = require('net');
// Setup a tcp server
var server = net.createServer(function (socket) {
socket.addListener("connect", function () {
console.log('hello');
sys.puts("Connection from " + socket.remoteAddress);
socket.end("Hello World\n");
});
});
server.listen(7000, "127.0.0.1");
console.log("TCP server listening on port 7000 at 127.0.0.1");
It started successfully, but how can I send some test msg to that tcp server, I tried SocketTest v.3 but not console output.
Use data event handler to receive data in the server.
socket.on('data', function(data) {
console.log(data);
// echo back
socket.write('You said '+data);
});
For people still looking for answers, here's how I did it with SocketTest
const net = require('net');
const client = new net.Socket();
const HOST = "127.0.0.1";
const PORT = 7000;
client.on('data', () => {
client.destroy(); // kill after response
});
client.on('error', () => {
client.destroy(); // kill
});
client.connect(PORT, HOST, () => {
client.write('Hello world!');
client.end();
});
I have srcds (source dedicated server)
at console add logaddress_add 0.0.0.0:25001
this turn on sending the log to the remote server
tried to catch the log in this way
var net = require('net');
var server = net.createServer(function(c) {
c.on('end', function() {
console.log('server disconnected');
});
c.pipe(c);
});
server.listen(25001);
and that
var net = require('net');
var client = net.connect({port: 25001});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
and that
var s = dgram.createSocket('udp4');
s.bind(25001, function(data) {
console.log(data)
});
no result. can someone help?
thanks in advance
[solved]
at SRCDS server
logaddress_add 0.0.0.0:8006 //for local ip
at app.js
var dgram = require('dgram'),
server = dgram.createSocket('udp4');
server.on('message', function (message, rinfo) {
var msg = message.toString('ascii').slice(5,-1);
console.log(msg);
});
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening ' + address.address + ':' + address.port);
});
server.bind(8006);
I ended up writing a little library to do this (srcds-log-receiver), which validates the packet format, extracts out the date and allows you to use the sv_logsecret function to put a small amount of authentication on the connection, since UDP packets are easily forged.
I also wrote a parser to turn those log lines in to useful objects.