UDP Scan Devices in LAN
-
First, open a UDP receiving port on the desktop.
-
Then send the following UDP broadcast to port 46953 in the LAN:
{"ip":"desktop ip address","port":50000} // port is the desktop listening port
-
The device will reply to the desktop's receiving port with:
{
"ip":"192.168.31.99",
"port":"46952",
"devname":"Device Name",
"deviceid":"123456789012345678901234567890123456790", // device UDID
"devsn":"XXXXXXXXXXXX", // device serial number
"devmac":"02:03:04:05:06:07", // device Wi‑Fi MAC address
"devtype":"iPhone8,1", // device model
"zeversion":"0.0.1.738", // service version
"sysversion":"9.0.2" // iOS version
} -
Desktop UDP scan example (Python 3.x):
# -*- coding: utf-8 -*-
import socket
import json
local_ip = '192.168.31.13' # desktop IP address
local_port = 31500
local = (local_ip, local_port)
remote = ("255.255.255.255", 46953)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(local)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(json.dumps({"ip": local_ip, "port": local_port}).encode('utf-8'), remote) # encode as UTF-8
while True:
data, addr = s.recvfrom(2048)
if not data:
print("client has exist")
break
print("received:", data, "from", addr)
s.close() -
Desktop UDP scan example (Node.js):
const http = require('http');
const dgram = require('dgram');
const server_Port = 31500; // UDP receive port
const client_Port = 31501; // UDP send port
const device_List = {}; // device list
// UDP server (receive)
const server_Socket = dgram.createSocket('udp4');
server_Socket.on('message', function(msg, rinfo){
console.log('received: %s', msg);
const device_Info = JSON.parse(msg);
if (device_Info.deviceid) {
device_List[device_Info.deviceid] = device_Info; // de-duplicate by deviceid
}
});
server_Socket.bind(server_Port);
// UDP client (send)
const client_Socket = dgram.createSocket('udp4');
client_Socket.bind(client_Port);
function search(){
const server_List = [];
const os = require('os');
const ifaces = os.networkInterfaces();
for (const dev in ifaces) {
ifaces[dev].forEach(function(details){
if (details.family === 'IPv4') {
server_List.push(details.address);
}
});
}
for (const address of server_List) {
const ip_ar = address.split(".");
const send2ip = `${ip_ar[0]}.${ip_ar[1]}.${ip_ar[2]}.255`;
const client_Socket = dgram.createSocket('udp4');
const msg = JSON.stringify({ip: address, port: server_Port});
client_Socket.send(msg, 0, msg.length, 46953, send2ip);
}
}
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'application/json'});
search();
response.end(JSON.stringify(device_List));
}).listen(22222);
search();
console.log('Web service at: http://127.0.0.1:22222/');