This should get you started. This Roku channel code simply listens for a request sent to port 42424 from a client then echoes the request back to the client:
Sub Main ()
port = CreateObject ("roMessagePort")
ui = CreateObject ("roVideoScreen")
ui.SetMessagePort (port)
ui.SetCertificatesFile ("common:/certs/ca-bundle.crt")
ui.SetLoop (True)
ui.SetContent ({
StreamFormat: "mp4",
Stream: {
Url: "https://ia600408.us.archive.org/26/items/BigBuckBunny_328/BigBuckBunny_512kb.mp4",
Bitrate: 581,
Quality: False,
StreamType: "mp4"
}
})
ui.Show ()
alive = aliveInit (port)
While True
msg = Wait (0, port)
If Type (msg) = "roVideoScreenEvent"
Print "roVideoScreenEvent"
' Handle roVideoScreenEvent here ...
Else If Type (msg) = "roSocketEvent"
alive.processEvent (msg)
EndIf
End While
End Sub
' Return an 'alive' object, which has a 'processEvent' method to handle socket event completions
Function aliveInit (port As Object) As Object
this = {}
' Use the same port used by the UI component
this.port = port
' Allocate a byte array for receiving data from, or sending data to, the client
this.buffer = CreateObject ("roByteArray")
this.bufSize = 512
this.buffer [this.bufSize - 1] = 0 ' Ensure space allocated in buffer
' Create a connnection-oriented stream socket
this.listenSocket = CreateObject ("roStreamSocket")
' Associate the message port with the socket
this.listenSocket.SetMessagePort (port)
' Define the socket address which will be bound with the socket
listenAddress = CreateObject ("roSocketAddress")
' Define which port will be used to listen for incoming connections
listenAddress.SetPort (42424)
' Bind the socket address with the socket
this.listenSocket.SetAddress (listenAddress)
' Ensure that a socket event will be generated when the socket becomes readable
this.listenSocket.NotifyReadable (True)
' Cause the bound socket to enter the listening state
this.listenSocket.Listen (2)
' Check that the socket is in the listening state
If Not this.listenSocket.IsListening ()
Print "Socket is not listening"
Return Invalid
EndIf
' Socket object for the socket returned by a connection
this.connectionSocket = Invalid
' Handle an roSocketEvent completion
this.processEvent = Function (msg As Object) As Void
' Socket event-handling loop
If m.listenSocket.IsListening () And m.listenSocket.eOK ()
' If the event occurs on the listening socket, it is for a new connection,
' otherwise for an existing connection
If msg.GetSocketID () = m.listenSocket.GetID () And m.listenSocket.IsReadable ()
' New connection
If m.connectionSocket <> Invalid
' If there is already an existing connection, terminate it
Print "Terminating existing connection"
m.connectionSocket.Close ()
m.connectionSocket = Invalid
EndIf
' Accept the new connection
m.connectionSocket = m.listenSocket.Accept ()
If m.connectionSocket = Invalid
Print "Accept failed"
Else
Print "Accepted new connection"
m.connectionSocket.NotifyReadable (True)
m.connectionSocket.SetMessagePort (m.port)
EndIf
Else If msg.GetSocketId () = m.connectionSocket.GetID () And m.connectionSocket.IsReadable ()
' Existing connection
If m.connectionSocket.IsReadable ()
' Read data from the client
recvLen = m.connectionSocket.Receive (m.buffer, 0, m.bufSize - 1)
Print "Bytes received: "; recvLen
If recvLen > 0
' Null terminate the input data so it can be printed
m.buffer [recvLen] = 0
Print "Echo input: "; m.buffer.ToAsciiString ()
' Send data back to the client
m.connectionSocket.Send (m.buffer, 0, recvLen)
EndIf
EndIf
If recvLen = 0 Or Not m.connectionSocket.eOK ()
m.connectionSocket.Close ()
m.connectionSocket = Invalid
EndIf
Else
Print "Unknown connection"
EndIf
EndIf
' Close the socket if something went wrong
If Not m.listenSocket.IsListening () Or Not m.listenSocket.eOK ()
Print "Socket loop exited"
m.listenSocket.Close ()
If m.connectionSocket <> Invalid
m.connectionSocket.Close ()
EndIf
EndIf
End Function
Return this
End Function
Here's some Python client code that just sends a "Hello" message to the Roku channel then prints out the echoed response:
import socket
# create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# now connect to the Roku on port 42424
s.connect(('192.168.0.6', 42424))
# send a poll request to the Roku
s.send(b'Hello')
# receive a response from the Roku
response = s.recv(512)
# print the Roku's response
print (response)
# terminate the socket
s.shutdown(socket.SHUT_RDWR)
s.close()