Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Cwright017
Visitor

Roku sockets

I'm trying to open a tcp socket from my Roku 3 for the purpose of reporting test results (instead of using the native debug console)

I've copied the example for roStreamSocket - but it doesn't work, at all.



'Run tests on brstest

function main()
print "MAIN...."
messagePort = CreateObject("roMessagePort")
connections = {}
buffer = CreateObject("roByteArray")
buffer[512] = 0
tcpListen = CreateObject("roStreamSocket")
tcpListen.setMessagePort(messagePort)
addr = CreateObject("roSocketAddress")
addr.setPort(54321)

tcpListen.setAddress(addr)
tcpListen.notifyReadable(true)
tcpListen.notifyWritable(true)

x = tcpListen.listen(4)

if not tcpListen.eOK()
print "Error creating listen socket"
stop
end if

while True
event = wait(0, messagePort)

if type(event) = "roSocketEvent"
print "GOT HERE"
changedID = event.getSocketID()

if changedID = tcpListen.getID() and tcpListen.isReadable()
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

end function



Is what I have in my main.brs - and it listens, and I seem to be able to telnet to the port - but it reports no connection event, nor does it report any data sent to it - and the event isn't of type string, but of type function 😕
0 Kudos
6 REPLIES 6
belltown
Roku Guru

Re: Roku sockets

I'm not sure why it didn't work for you. I ran your code (unmodified), then typed telnet 192.168.0.6 54321 followed by hello. The debug output from the code (on port 8085) showed:


------ Compiling dev 'Socket Test' ------

------ Running dev 'Socket Test' main ------
MAIN....
GOT HERE
accepted new connection 87031814
GOT HERE
received is 1
Echo input: 'h'
GOT HERE
received is 1
Echo input: 'e'
GOT HERE
received is 1
Echo input: 'l'
GOT HERE
received is 1
Echo input: 'l'
GOT HERE
received is 1
Echo input: 'o'


Each character typed was echoed back to the telnet console on port 54321.

What output are you seeing from the debugger on port 8085?
0 Kudos
Cwright017
Visitor

Re: Roku sockets

Hey thanks for the quick reply.

My output is just
MAIN...


and when I telnet I get


$ telnet 10.65.94.89 54321
Trying 10.65.94.89...
Connected to 10.65.94.89.
Escape character is '^]'.
hello


but no output on the debug port - not even a connection event.

What firmware are you running? I'm using 7.1 build 4061. Just tried on a Roku 4 and that too didn't work 😕
0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: Roku sockets

"Cwright017" wrote:
I'm trying to open a tcp socket from my Roku 3 for the purpose of reporting test results (instead of using the native debug console)

I've copied the example for roStreamSocket - but it doesn't work, at all.

...

Is what I have in my main.brs - and it listens, and I seem to be able to telnet to the port - but it reports no connection event, nor does it report any data sent to it - and the event isn't of type string, but of type function 😕


Are you literally running just the above code, or are you running in a larger context with some other code?

If the latter, make sure you don't have a function/sub named 'event' somewhere. 🙂
0 Kudos
EnTerr
Roku Guru

Re: Roku sockets

"RokuKC" wrote:
If the latter, make sure you don't have a function/sub named 'event' somewhere. 🙂

Oooh, that's a good one! It throws us at the name scoping question, doesn't it.
0 Kudos
Cwright017
Visitor

Re: Roku sockets

Argh - I did have another function called event! It now all works fine.

But on the topic of namespacing - is this something Roku plan to introduce in the near future? Maybe classes?
0 Kudos
chaklasiyanikun
Roku Guru

Re: Roku sockets

Hello belltown,


I tried to make a web server in the Roku Application. I used the above code is below here.

Here It crashes after Print Start While Loop.

sub init()
    ? "Start Main()"
    m.port = CreateObject("roMessagePort")
    connections = {}
    buffer = CreateObject("roByteArray")
    buffer[512] = 0
    tcpListen = CreateObject("roStreamSocket")
    tcpListen.SetMessagePort(m.port)
    addr = CreateObject("roSocketAddress")
    addr.setPort(54321)
    ? "addr.IsAddressValid() : " addr.IsAddressValid() 
tcpListen.setAddress(addr) ' Here Automatic set 0.0.0.0 IP tcpListen.notifyReadable(true) tcpListen.notifyWritable(true)
x = tcpListen.listen(5) if not tcpListen.eOK() ? "Error creating listen socket" STOP end if while true ? "Start While Loop" msg = Wait(0, m.port) ? "msg : "msg ? "Type(msg) : " Type(msg) if (Type(msg) = "roSocketEvent") ? "GOT HERE" changedID = msg.getSocketID() if changedID = tcpListen.getID() and tcpListen.isReadable() newConnection = tcpListen.accept() if newConnection = invalid ? "accept failed" else ? "accepted new connection " newConnection.getID() newConnection.notifyReadable(true) newConnection.SetMessagePort(m.port) 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) ? "received is " received if received > 0 connection.send(buffer, 0, received) else if received = 0 ' client closed closed = true end if end if if closed or not connection.eOK() ? "closing connection " changedID connection.close() connections.delete(StrI(changedID)) end if end if end if ? "End While Loop" end while ? "Main loop exited" tcpListen.close() for each id in connections connections[id].close() end for ? "End Main()" end sub

I tried to Run this code in Roku Premiere +. I also Read the namming Scope. But I dont know why it doesn't work. you have any idea about this.

Thank you

0 Kudos