6

I have created a server using net module:

// Creating and connecting with server
const net = require('net');
const server = net.createServer(); //Creating server

//Connecting with server
server.on('connection', function (socket) {
    let remoteAddress = `${socket.remoteAddress},${socket.remotePort}`

    console.log(remoteAddress)
    console.log(`connection is established... ${Date.now()} \n `);
    socket.write(`connection is established...${Date.now()} \n`);
    //Receiving and Sending payload from/to client
    socket.on('data', async function (payload) {
        console.log("payload from client",payload)
        socket.write(`acknowledge : ${payload}`);
    });
    //Close connection
    socket.on('close', function () {
        console.log('Server Connection Closed');
    });
    //Server error
    socket.on('error', function (err) {
        console.log("Caught flash policy server socket error: ")
        console.log(err.stack)
    });
});

server.listen(8001, function () {
    console.log('Server Listing on Port 8001');
 })

I deploy this code to AWS EC2 and When I tried to connect with telnet client (telnet ec2_ip 8001), initailly it is working but after sometime it is giving following errors.

screenshot of telnet client: enter image description here

screenshot of ec2 logs: enter image description here

And When I tried to connect with real IOT scooter with ec2 Ip address and port 8001, It is not connecting for even a second. enter image description here

Can anyone please tell me what I am doing wrong?

Note: I don't have much knowledge about IoT. This is the first time I am connecting an IoT scooter with nodejs.

manzt
  • 454
  • 2
  • 12
  • can you use port number 23 as mentioned here:- https://www.rfwireless-world.com/Terminology/TELNET-port-number.html#:~:text=Telnet%20uses%20TCP%20protocol%20for,and%20display%20output%20on%20screen. – man.in.the.jukebox Sep 08 '22 at 15:42
  • Why is the server log showing HTTP OPTIONS/GET requests as well? – robertklep Sep 13 '22 at 14:13
  • Those logs are of express API, ignore them. @robertklep – manzt Sep 14 '22 at 07:03
  • I don't think, there is anything to do with port 23 in this case. I am running server on port 8001 and telnet need two thing IP address and port number on which server is running @man.in.the.jukebox – manzt Sep 14 '22 at 07:10

1 Answers1

0

It is hard to know exactly what your problem is without seeing more of your code, or getting more explained. Like what happens in your real code? The logs seem to indicate that you are making outbound calls to another service after the initial connection. If that is the case, are you handling error conditions of your dependencies correctly? An unhandled exception would kill your server, and it would need to be restarted to work again. If you are using dependencies that fail, for instance databases, other servers, configuration files etc that fail on your scooter, that may be why it fails without even allowing one single connection. But without knowing more specifics about your application and architecture, it is just wild guesses at this point.

Your server example code, seems fine. As long as you have opened for traffic on that port in your routing configuration in AWS, it should respond. It could be that you just simply run out of connections, but again that requires me to know more to give you a definitive answer.

My advice would be to check your dependencies, and your logs for supporting systems.

Espen
  • 2,456
  • 1
  • 16
  • 25