简体   繁体   中英

Send the Hostname of a TCP client to the Server in Node JS

I have a Master server machine (windows 10), which I'm using to control several Raspberry PIs using TCP in node js.

I'm wondering what would be the best way to send the hostname of each pi (pi-001.local, pi002.local, pi003.local etc...) to the server.

running socket.remoteAddress on the Server returns the IP of the client, but what I need the Hostname

Obviously each pi can resolve its own hostname using the os.hostname() method but I need to keep track and know what hostname each net.Socket object is associated with on the Server side .

Is there any better way than just sending it via socket.write(os.hostname()) ?

thanks!

As long as the DNS server used by the TCP server knows about the hostnames of the PI devices, you could just have the TCP server do a reverse lookup of socket.remoteAddress for each connection. You can do this reverse lookup in node with dns.reverse() .

This is already somewhat done in the SMTP protocol, which is a good model to use for TCP negotiation. Using that type of approach, the first thing your Pi's would do when they connect to the server is write a

HELO pi00001.local

... that way your server always expects the information up-front from the Pi with each connection or reconnect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM