1. --!movie
  2. --!encoding=utf-8
  3.  
  4. --****************************************************************************
  5. -- @file      TcpServer Demo
  6. -- @author    Valentin Schmidt
  7. -- @version   0.1
  8. --****************************************************************************
  9.  
  10. -- Usage
  11. -- =====
  12. -- Start this demo, click button "(Re)Start TCP-Server" and then open a DOS box
  13. -- and type:
  14. --
  15. --   telnet localhost 12345
  16. --
  17. -- Whatever you now type into the DOS Box will be echoed (in realtime) in
  18. -- the Message Window of this demo.
  19.  
  20. -- Alternatively you can also use nc ("NetCat for Windows") and in the DOS box
  21. -- type:
  22. --
  23. --   nc localhost 12345
  24. --
  25. -- In contrast to telnet, entered data now is not sent immediately, but only
  26. -- when the <Enter> key was pressed
  27.  
  28. -- Note: instead of 'put', this demo uses function printMsg() provided by CommandLine Xtra.
  29. -- printMsg() allows to write to the Message Window without automatically added line breaks.
  30.  
  31. global $
  32.  
  33. global server
  34. global sockets
  35.  
  36. ----------------------------------------
  37. -- START
  38. ----------------------------------------
  39. on startMovie
  40.  
  41.   -- libs
  42.   $.import("qt-webkit")
  43.  
  44.   _player.debugPlaybackEnabled = 1
  45.  
  46.   printMsg("To test the TCPServer, open a DOS box and type:"&RETURN&RETURN)
  47.   printMsg("  telnet localhost 12345"&RETURN&RETURN)
  48.   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)
  49.  
  50.   -- window settings
  51.   _movie.stage.title = "QTcpServer Demo"
  52.   _movie.stage.titlebarOptions.visible = TRUE
  53.   _movie.stage.rect = rect(0,0,320,240)
  54.   _movie.stage.bgColor = rgb(212,208,200)
  55.   _movie.centerStage = 1
  56.  
  57.   -- create a button
  58.   m = new(#button)
  59.   m.rect = rect(0,0,160,240)
  60.   m.alignment = "center"
  61.   m.fontsize = 12
  62.   m.fontStyle = "bold"
  63.   m.text = "(Re)Start TCP-Server"
  64.   m.scripttext = "on mouseDown"&RETURN&"startTcpServer()"&RETURN&"end"
  65.  
  66.   -- create sprite, assign button
  67.   _movie.puppetSprite(1, TRUE)
  68.   sprite(1).member = m
  69.   sprite(1).loc = point(80, 80)
  70.  
  71.   -- create a button
  72.   m = new(#button)
  73.   m.rect = rect(0,0,160,240)
  74.   m.alignment = "center"
  75.   m.fontsize = 12
  76.   m.fontStyle = "bold"
  77.   m.text = "Stop TCP-Server"
  78.   m.scripttext = "on mouseDown"&RETURN&"stopTcpServer()"&RETURN&"end"
  79.  
  80.   -- create sprite, assign button
  81.   _movie.puppetSprite(2, TRUE)
  82.   sprite(2).member = m
  83.   sprite(2).loc = point(80, 120)
  84.  
  85.   -- force immediate update
  86.   _movie.updateStage()
  87.  
  88.   -- show the window
  89.   _movie.stage.visible = 1
  90.  
  91.   -- create the TCP server
  92.   server = $.Qt.TcpServer.new()
  93.  
  94.   -- slot called whenever a new TCP connection was made
  95.   server.connect(#newConnection, _movie, #slotNewConnection)
  96.  
  97.   -- list of open sockets
  98.   sockets = []
  99. end
  100.  
  101. ----------------------------------------
  102. -- starts the TCP server
  103. ----------------------------------------
  104. on startTcpServer (host, port)
  105.   if voidP(host) then host = "127.0.0.1"
  106.   if voidP(port) then port = 12345
  107.   stopTcpServer()
  108.   ok = server.listen(host, port)
  109.   if not ok then
  110.     printMsg(RETURN&"> Error: Could not start TCP Server (host="&host&", port="&port&")"&RETURN)
  111.     return
  112.   end if
  113.   printMsg(RETURN&"> Notice: TCP Server started (host="&host&", port="&port&")"&RETURN)
  114. end
  115.  
  116. ----------------------------------------
  117. -- stops the TCP server (and closes all open sockets)
  118. ----------------------------------------
  119. on stopTcpServer
  120.   if not server.isListening() then return
  121.   printMsg(RETURN&"> Notice: TCP Server stopped"&RETURN)
  122.  
  123.   -- close all open sockets
  124.   repeat with socket in sockets
  125.     socket.close()
  126.   end repeat
  127.   sockets = []
  128.  
  129.   -- close the server
  130.   server.close()
  131. end
  132.  
  133. ----------------------------------------
  134. -- @callback
  135. ----------------------------------------
  136. on slotNewConnection ()
  137.   printMsg(RETURN&"> Notice: new TCP connection"&RETURN)
  138.  
  139.   socket = server.nextPendingConnection()
  140.   socket.connect(#readyRead, _movie, #slotReadyRead)
  141.  
  142.   -- show hello message to connected client
  143.   socket.write("Welcome to the Lingo QTcpServer!"&numtochar(13)&numtochar(10)&numtochar(13)&numtochar(10))
  144.  
  145.   -- add to table of open sockets
  146.   sockets.add(socket)
  147. end
  148.  
  149. ----------------------------------------
  150. -- prints received data to message window
  151. -- @callback
  152. ----------------------------------------
  153. on slotReadyRead (socket)
  154.   size = socket.bytesAvailable()
  155.   data = socket.read(size)
  156.   if data.length then
  157.     -- OEM to UTF-8 conversion for using "telnet localhost 12345" in a Win XP DOS box as client
  158.     -- Change this to the actual codepage used by your TCP client
  159.     printMsg(data.readRawString(data.length, "ibm850"))
  160.   end if
  161. end
  162.  
[raw code]