--!movie
--!encoding=utf-8
--****************************************************************************
-- @file TcpServer Demo
-- @author Valentin Schmidt
-- @version 0.1
--****************************************************************************
-- Usage
-- =====
-- Start this demo, click button "(Re)Start TCP-Server" and then open a DOS box
-- and type:
--
-- telnet localhost 12345
--
-- Whatever you now type into the DOS Box will be echoed (in realtime) in
-- the Message Window of this demo.
-- Alternatively you can also use nc ("NetCat for Windows") and in the DOS box
-- type:
--
-- nc localhost 12345
--
-- In contrast to telnet, entered data now is not sent immediately, but only
-- when the <Enter> key was pressed
-- Note: instead of 'put', this demo uses function printMsg() provided by CommandLine Xtra.
-- printMsg() allows to write to the Message Window without automatically added line breaks.
global $
global server
global sockets
----------------------------------------
-- START
----------------------------------------
on startMovie
-- libs
$.import("qt-webkit")
_player.debugPlaybackEnabled = 1
printMsg("To test the TCPServer, open a DOS box and type:"&RETURN&RETURN)
printMsg(" telnet localhost 12345"&RETURN&RETURN)
printMsg("And hit the <Enter>key. Everything you now type into the"&RETURN&"DOS Box, will be echoed (in realtime) in this Message Window."&RETURN)
-- window settings
_movie.stage.title = "QTcpServer Demo"
_movie.stage.titlebarOptions.visible = TRUE
_movie.stage.rect = rect(0,0,320,240)
_movie.stage.bgColor = rgb(212,208,200)
_movie.centerStage = 1
-- create a button
m = new(#button)
m.rect = rect(0,0,160,240)
m.alignment = "center"
m.fontsize = 12
m.fontStyle = "bold"
m.text = "(Re)Start TCP-Server"
m.scripttext = "on mouseDown"&RETURN&"startTcpServer()"&RETURN&"end"
-- create sprite, assign button
_movie.puppetSprite(1, TRUE)
sprite(1).member = m
sprite(1).loc = point(80, 80)
-- create a button
m = new(#button)
m.rect = rect(0,0,160,240)
m.alignment = "center"
m.fontsize = 12
m.fontStyle = "bold"
m.text = "Stop TCP-Server"
m.scripttext = "on mouseDown"&RETURN&"stopTcpServer()"&RETURN&"end"
-- create sprite, assign button
_movie.puppetSprite(2, TRUE)
sprite(2).member = m
sprite(2).loc = point(80, 120)
-- force immediate update
_movie.updateStage()
-- show the window
_movie.stage.visible = 1
-- create the TCP server
server = $.Qt.TcpServer.new()
-- slot called whenever a new TCP connection was made
server.connect(#newConnection, _movie, #slotNewConnection)
-- list of open sockets
sockets = []
end
----------------------------------------
-- starts the TCP server
----------------------------------------
on startTcpServer (host, port)
if voidP(host) then host = "127.0.0.1"
if voidP(port) then port = 12345
stopTcpServer()
ok = server.listen(host, port)
if not ok then
printMsg(RETURN&"> Error: Could not start TCP Server (host="&host&", port="&port&")"&RETURN)
return
end if
printMsg(RETURN&"> Notice: TCP Server started (host="&host&", port="&port&")"&RETURN)
end
----------------------------------------
-- stops the TCP server (and closes all open sockets)
----------------------------------------
on stopTcpServer
if not server.isListening() then return
printMsg(RETURN&"> Notice: TCP Server stopped"&RETURN)
-- close all open sockets
repeat with socket in sockets
socket.close()
end repeat
sockets = []
-- close the server
server.close()
end
----------------------------------------
-- @callback
----------------------------------------
on slotNewConnection ()
printMsg(RETURN&"> Notice: new TCP connection"&RETURN)
socket = server.nextPendingConnection()
socket.connect(#readyRead, _movie, #slotReadyRead)
-- show hello message to connected client
socket.write("Welcome to the Lingo QTcpServer!"&numtochar(13)&numtochar(10)&numtochar(13)&numtochar(10))
-- add to table of open sockets
sockets.add(socket)
end
----------------------------------------
-- prints received data to message window
-- @callback
----------------------------------------
on slotReadyRead (socket)
size = socket.bytesAvailable()
data = socket.read(size)
if data.length then
-- OEM to UTF-8 conversion for using "telnet localhost 12345" in a Win XP DOS box as client
-- Change this to the actual codepage used by your TCP client
printMsg(data.readRawString(data.length, "ibm850"))
end if
end