R
R
rail01y2017-09-10 04:49:10
UDP
rail01y, 2017-09-10 04:49:10

How to correctly implement a search for a device on the network?

Good day.
In view of the fact that I am not familiar with administration in general and the network structure in particular, I try to solve the following question: how to implement a search for a device on the network?

More specifically, the situation is as follows: I am doing a small application on react and would like to simplify the search for devices within the same subnet. I imagine the process itself as follows, the server sends some kind of message (of the hello type) to the broadcast, when the client turns on the application, it starts listening and catching udp packets, thereby finding the server that broadcasts and then normal work enters the scheme, but I can’t do it, I give the code below:

a demo server that sends out:

from socket import *
import time


cs = socket(AF_INET, SOCK_DGRAM)
while(True):
  cs.sendto(b'This is a test', ('0.0.0.0', 12345))
  print('test')
  time.sleep(1)


application that listens:
import DGRAM from 'dgram';
let udpSocket = DGRAM.createSocket('udp4');
udpSocket.bind(12345, '0.0.0.0');
udpSocket.on('message', (msg, info) => {
            console.log(msg, info)
});


the server itself sends the message (if I start monitoring the message locally nc -u -l 12345 then I see them), but the application does not catch them until I specify the physical address where to send it. Who solved this issue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rail01y, 2017-09-10
@rail01y

Actually, I solved the problem, in the end it was necessary to specify the server options when sending (SO_BROADCAST https://www.ibm.com/support/knowledgecenter/en/SSL... - description here)
the server code itself:

from socket import *
import time

cs = socket(AF_INET, SOCK_DGRAM)
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

while(True):
  cs.sendto("TestText".encode(), ('255.255.255.255', 12345))
  print('test')
  time.sleep(1)

the address in the application also began to look at 255.255.255.255

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question