UDP Broadcasts

Hi all, I’m trying to send a UDP broadcast on the local network in order to discover my server. I wrote a udp client/server in c, and it works. Now, I’m trying to convert the client to brighscript, but I can’t get it working. It seems that the server never receives any packets. Right now I’ve stripped the code down to just sending the packet. I then watch the server output to see if the packet is received – it never is.

Here’s the code I’m using:


function Discover()
    SEND_PORT = "8881"
    KEY_SEND = "SeCrEt"
    
    msgPort = CreateObject("roMessagePort")
    sendSock = CreateObject("roDatagramSocket")
    sendSock.SetMessagePort(msgPort)
    
    
    sendAddr = CreateObject("roSocketAddress")
    sendAddr.setAddress("255.255.255.255:"+SEND_PORT)
    
    sendSock.setSendToAddress(sendAddr)
    sendSock.setBroadcast(true)
    sendSock.notifyReadable(true)

    data = CreateObject("roByteArray")
    data.FromAsciiString(KEY_SEND)
    
    ' Sanity Check
    ?"Data count: "+data.Count().tostr()
    
    sendSock.send(data, 0, data.Count())
    
    ' Check error
    if sendSock.eAgain()
        ?"E_AGAIN"
        return false
    else if sendSock.eAlready()
        ?"E_ALREADY"
        return false
    else if sendSock.eDestAddrReq()
        ?"E_DEST_ADDR_REQ"
        return false
    else if sendSock.eHostUnreach()
        ?"E_HOST_UNREACH"
        return false
    else if sendSock.eInProgress()
        ?"E_IN_PROGRESS"
        return false
    else if sendSock.eOk()
        ?"E_OK"
        
    else if sendSock.eSuccess()
        ?"E_SUCCESS"
        
    else if sendSock.eWouldBlock()
        ?"E_WOULD_BLOCK"
        return false
    end if
    
continue:
    while true
        event = wait(1000, msgPort)
        if event = invalid
            ?"Recevieved invalid event"
            goto continue       
        end if
        
        if type(event) <> "roSocketEvent"
            ?"Received unknown type"
            goto continue
        end if
        
        ?"Message: " + event.GetMessage()
    end while
end function

Can anyone see the problem? Any help would be greatly appreciated. Cheers!

Update: I’ve changed the sendToAddress to the network broadcast address, and that seems to work. Changing it to 255.255.255.255 is what breaks it. Any ideas why this is happening? Or alternative approaches?

Here’s the updated code:


function Discover()
    SEND_PORT = "8881"
    KEY_SEND = "SeCrEt"
    
    msgPort = CreateObject("roMessagePort")
    sock = CreateObject("roDatagramSocket")
    sock.SetMessagePort(msgPort)
    
    addr = CreateObject("roSocketAddress")
    addr.setAddress("0.0.0.0:8882")
    sock.setAddress(addr)
    
    sendAddr = CreateObject("roSocketAddress")
    sendAddr.setAddress("192.168.128.255:"+SEND_PORT)
    
    sock.setSendToAddress(sendAddr)
    sock.setBroadcast(true)
    sock.notifyReadable(true)

    data = CreateObject("roByteArray")
    data.FromAsciiString(KEY_SEND)
    
    ' Sanity Check
    ?"Data count: "+data.Count().tostr()
    
    sock.send(data, 0, data.Count())
    
    ' Check error
    if sock.eAgain()
        ?"E_AGAIN"
        return false
    else if sock.eAlready()
        ?"E_ALREADY"
        return false
    else if sock.eDestAddrReq()
        ?"E_DEST_ADDR_REQ"
        return false
    else if sock.eHostUnreach()
        ?"E_HOST_UNREACH"
        return false
    else if sock.eInProgress()
        ?"E_IN_PROGRESS"
        return false
    else if sock.eOk()
        ?"E_OK"
        
    else if sock.eSuccess()
        ?"E_SUCCESS"
        
    else if sock.eWouldBlock()
        ?"E_WOULD_BLOCK"
        return false
    end if
    
continue:
    while true
        event = wait(10000, msgPort)
        if event = invalid
            ?"Recevieved invalid event (Timeout)"
            goto continue       
        end if
        
        if type(event) <> "roSocketEvent"
            ?"Received unknown type"
            goto continue
            
        end if
        
        if event.GetSocketID() = sock.GetID()
            if sock.isReadable()
                message = sock.receiveStr(68)
                ?"Message: "+message
            end if
        end if
    end while
end function

Cheers!

Can I access the subnet mask? That would let me get the network broadcast address myself…

Anyone?

Can any of the roku dev’s pitch in with some thoughts? How can I send a UDP broadcast to the LAN? I’m really stuck on this one.

Right now we don’t have a good solution for this. You could try this workaround:

look at the local address and guess:
a. the /24 (sub)network
b. the natural network implied by the non-routable address.

We hope to provide a better way to do this in a future release.

–Mark

“RokuMarkn” wrote:
Right now we don’t have a good solution for this. You could try this workaround:

look at the local address and guess:
a. the /24 (sub)network
b. the natural network implied by the non-routable address.

We hope to provide a better way to do this in a future release.

–Mark
Ah, I see. Ok, well I’ll explore using multicasting instead then. Cheers :slightly_smiling_face:

Any chance this made the cut? It would be great to obtain the subnet as part of roDeviceInfo.GetConnectionInfo, but any access would be appreciated.

“RokuMarkn” wrote:
Right now we don’t have a good solution for this. You could try this workaround:

look at the local address and guess:
a. the /24 (sub)network
b. the natural network implied by the non-routable address.

We hope to provide a better way to do this in a future release.

–Mark