Forum Discussion

Uzair_Abdullah's avatar
Uzair_Abdullah
Streaming Star
2 years ago

Run socket with external IP

I am trying to send data, from some server, to my Roku. I am using the exact code given in docs to listen through TCP. When requested from IP of Roku, the server says connection refused (this is all being done through a server set up locally). When send through external IP, no logs come and the server just times out. How to fix this?
Here's the code from docs:

    messagePort = CreateObject("roMessagePort")
    connections = {}
    buffer = CreateObject("roByteArray")
    buffer[512] = 0
    tcpListen = CreateObject("roStreamSocket")
    tcpListen.setMessagePort(messagePort)
    addr = CreateObject("roSocketAddress")
    addr.setPort(12345)
    tcpListen.setAddress(addr)
    tcpListen.notifyReadable(true)
    tcpListen.listen(4)
    if not tcpListen.eOK()
        print "Error creating listen socket"
        stop
    else
        print "Listening"
    end if

    while(true) 'Message Port that fires every 7 seconds to change value of MyField
        event = wait(0, messagePort)
        if type(event) = "roSocketEvent"
            changedID = event.getSocketID()
            if changedID = tcpListen.getID() and tcpListen.isReadable()
                ' New
                newConnection = tcpListen.accept()
                if newConnection = invalid
                    print "accept failed"
                else
                    print "accepted new connection " newConnection.getID()
                    newConnection.notifyReadable(true)
                    newConnection.setMessagePort(messagePort)
                    connections[Stri(newConnection.getID())] = newConnection
                end if
            else
                ' Activity on an open connection
                connection = connections[Stri(changedID)]
                closed = False
                if connection.isReadable()
                    received = connection.receive(buffer, 0, 512)
                    print "received is " received
                    if received > 0
                        print "Echo input: '"; buffer.ToAsciiString(); "'"
                        ' If we are unable to send, just drop data for now.
                        ' You could use notifywritable and buffer data, but that is
                        ' omitted for clarity.
                        connection.send(buffer, 0, received)
                    else if received = 0 ' client closed
                        closed = True
                    end if
                end if
                if closed or not connection.eOK()
                    print "closing connection " changedID
                    connection.close()
                    connections.delete(Stri(changedID))
                end if
            end if
        end if
    end while

    print "Main loop exited"
    tcpListen.close()
    for each id in connections
        connections[id].close()
    end for
No RepliesBe the first to reply