如何加入Rebol中的SSDP组播组?

时间:2017-02-03 22:50:14

标签: rebol rebol2 ssdp

我试图收听SSDP多播消息,例如NOTIFY和SEARCH。

这是我的代码,但即使有线路人看到它们,我也没有看到这些消息。那么,如何加入SSDP组播组并接收消息?

Rebol []

attempt [close ssdp]
local-ip: read join dns:// read dns://

ssdp: open/binary udp://:8000
probe group: compose/deep [multicast-groups: [[235.255.255.250 (local-ip)]]]
set-modes ssdp group

forever [
    port: wait [ssdp]
    probe data: copy port
]

1 个答案:

答案 0 :(得分:1)

以下代码首先发送SSDP SEARCH命令以拾取网络上的所有设备,然后侦听来自其他设备的SEARCH命令。<​​/ p>

REBOL [ 
    Notes: {to listen for SSDP messages.  Works on Rebol2}   
] 

local-ip: read join dns:// read dns:// 

probe local-ip 

attempt [close odata] 
attempt [close idata] 

odata: open/binary udp://239.255.255.250:1900 ; SSDP multicast port address 
set-modes odata [multicast-ttl: 10]
; next line seems unnecessary
; set-modes odata compose/deep [multicast-interface: (local-ip)] 

idata: open/binary udp://:1900 
set-modes idata compose/deep [multicast-groups: [[239.255.255.250 (local-ip)]]] 

ST: "ssdp:all" 
MX: 3 

insert odata rejoin [ 
         {M-SEARCH * HTTP/1.1} crlf 
         {HOST: 239.255.255.250:1900} crlf 
         {MAN: "ssdp:discover"} crlf 
         {MX: } MX crlf 
         {ST: } ST crlf 
         crlf 
] 

forever [ 
    port: wait [odata idata] 
    data: copy port 
    if find/part data {M-SEARCH} 8 [
        print "SSDP search issued from:"
        print [ "Address: " port/remote-ip]
        print [ "On port: " port/remote-port]
    ]
    probe data
]