Trouble Catching a UDP packet DGRAM - javascript

I have an arduino on LAN emitting UDP packets with some data.
From my laptop using an ssh session with a Pi, both my laptop and Pi are also on the LAN, when I run:
sudo tcpdump -n udp port 8888
Terminal registers the UDP packets
18:14:44.868050 IP 192.168.1.27.8888 > 255.255.255.255.67: BOOTP/DHCP, unknown (0x1a), length 4
I'm a little unsure why it seems that the packet arrives at 255.255.255.255 on port 67 since my arduino is programmed to send to port 8888. The Pi is running the following script:
const dgram = require('dgram');
const catcher = dgram.createSocket('udp4');
catcher.on('listening', () => {
const address = catcher.address();
console.log(`server listening ${address.address}:${address.port}`);
});
catcher.on('message', (msg, rinfo) => {
console.log('Received a Message from: ' + rinfo.address + " Message Code: " + msg.readUInt8(0));
});
catcher.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
});
catcher.bind({
address: "255.255.255.255",
port: 8888,
exclusive: true
});
Which is pretty much identical to the dgram basic documentation but I don't actually see anything show up in the terminal for the packets. I've tried cycling through binding to localhost, define it's local IP manually, when it's just left at catcher.bind(8888) it's bound to 0.0.0.0, and lastly I've tried 255.255.255.255 but no luck. Even tried port 67 just be sure.
Obviously the packets are reaching the Pi as I can see them through tcpdump but I'm unclear as to why they're not getting picked up.
Would any of you know? Cheers!

Related

How to handle partial connection?

I try to create an reverse proxy that work bidirectional. its different from apache that stateless reverse proxy. its means, device can host webserver without public ip. so far i can reverse static content without any problem, css, javascript. but, something like media that return 206 code. it broken in middle. it stop reverse proxy, and try to keep run the server again and again. and i got the video load full. what the problem? i think, it okay already. cause, i jsut forward everything as binary. not specific coding.
reverse proxy port 80 forward to express server in port 8080
import net from 'net';
const server = new net.Server();
server.on('connection', socket => {
socket.on('data', data => {
const client = new net.Socket();
client.connect({
host: '127.0.0.1',
port: 8080
});
client.write(data);
client.on('data', data => {
socket.write(data)
})
})
})
server.listen(80, () => {
console.log('server is running')
});

Javascript: Can I open websocket server connection at random port

I want to create webserver socket connection at random port. And I want to return server port to calling application or just print it in terminal.
The typical code to create a server connection is as below.
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 0 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
So I am trying to create server at port 0. I assume it will create server at random port. How do I get that random port?
I want to know the port number, as soon as server socket is created.
Now I am able to create the websocket server at random port and able to get the port number as well. Not sure if it is the right way, but it works.
const http = require('http');
const WebSocket = require('ws');
const url = require('url');
const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });
wss.on('connection', function connection(ws) {
console.log(wss);
});
server.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
});
server.listen(0, '127.0.0.1', function incoming() {console.log (server.address().port);});
Websocket works with http/s on port 80 or 443. The server may listen on any port it chooses, but if it chooses any port other than 80 or 443, it may have problems with firewalls and/or proxies. Browsers generally require a secure connection for WebSockets, although they may offer an exception for local devices.

What does socket remote port represent?

http-client.js:
const http = require('http');
http.get
(
{
port : 9001,
host : 'localhost'
},
(res) =>
{
//...
}
);
tcp-server.js:
const net = require('net');
let server = new net.Server();
server.listen(9001, 'localhost', (err) =>
{
console.log('Started listening', server.address());
});
server.on('connection', (sock) =>
{
console.log(`Connected ${sock.remoteAddress}:${sock.remotePort}`);
});
I run node tc-server.js and then when I run node http-client.js I see output like:
Started listening { address: '127.0.0.1', family: 'IPv4', port: 9001 }
Connected 127.0.0.1:59506
I close http-client.js and run node http-client.js again. I see: Connected 127.0.0.1:59508
I close server and run again, and run the client again, I see Connected 127.0.0.1:59510
So the socket.remotePort is increasing all the time. What I don't understand is why those numbers for ports, I was expecting to see 9001 for port number since that's where the http request was being sent and successfully reached the listening tcp server.
Both sides of a TCP conversation have to have an address and a port. E.g., clients use ports too. What your console.log was telling you was that the client connected to your port 9001 using its port 59506. When your server sends packets to the client, it addresses them with the client's address and that port number, so the TCP layer of the network stack on the client knows what process to send the packet to. (More in the Wikipedia article on TCP.) You see the number increasing just as a byproduct of how your client system assigns available ports to connections.
You don't normally need to care about the client's port.

NodeJS websocket server on Plesk doesn't answer

I am new to NodeJS and I have just set up a subdomain to work with it on my Plesk Onyx 17.5.3 server.
I have done a simple websockets chat app but it doesn't work.
If I start the app via command line doing:
node server/server.js
the app works flawlessly. The code in server.js is:
"use strict";
process.title = 'node-chat';
const WebSocketServer = require('ws').Server;
const PORT = 9000;
const wss = new WebSocketServer({port: PORT});
console.log('WSS');
let messages = [];
wss.on('connection', function (ws) {
console.log('WS connection');
messages.forEach(function(message){
ws.send(message);
});
ws.on('message', function (message) {
messages.push(message);
console.log('Message Received: %s', message);
wss.clients.forEach(function (conn) {
conn.send(message);
});
});
});
wss.on('error', function(obj){
console.log('WS error');
console.log(obj);
});
console.log((new Date()) + 'server.js started');
If I start the application using Plesks "Restart app" it doesn't work. Doing a ps aux I can see the process is working. In the log file I see it has started:
App 17579 stdout: WSS
App 17579 stdout: Fri Jul 28 2017 13:52:44 GMT+0200 (CEST)server.js started
But there is no log saying websocket server has started or crashed, it just doesn't work. If I try to connect a client side js app to the server gives an error saying it can't connect to the server:
WebSocket connection to 'ws://server_address:9000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Any clues?
Thanks!
May I ask where you found the Nodejs logs on Plesk? And how are you able to get to the console on plesk?
As an answer to your question:
It could be that the port you provided is not configured to allow traffic from the web to your node application. That's why I'd recommend using
var port = process.env.PORT || 9000;
This line will try to use the port that could be configured in an object containing the user environment data.

Open port 23 to read data using Node.js

On my windows laptop there is a program with a TCP/IP server on port 23. I can open it with a telnet terminal and see the data streaming. I need to get that data into a node.js program I'm working on. Should be easy but I haven't found any code examples. Searches come up with lots of examples of how to make a server on port 23 with Node.js.
Thanks
This is a high level TCP/IP socket implementation in node. See: Node net API
var net = require('net'),
port = 23,
host = 'localhost',
socket = net.createConnection(port, host);
socket
.on('data', function(data) {
console.log('received: ' + data);
})
.on('connect', function() {
console.log('connected');
})
.on('end', function() {
console.log('closed');
});

Categories

Resources