Skip to content
This repository has been archived by the owner on Oct 29, 2022. It is now read-only.

Commit

Permalink
Added sample for Console and Socket modules.
Browse files Browse the repository at this point in the history
Added sample for Console and Socket modules.
  • Loading branch information
Rinnegatamante committed Sep 27, 2015
1 parent 3fd23f3 commit 4b85381
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions samples/console_and_socket.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
-----------------------------------------------------------------------------
-- This sample works fine with PC utility, CHMM2 sender --
-- You can find the source of this utility here: --
-- https://github.com/Rinnegatamante/CHMM2/blob/master/tools/sender/main.c --
-----------------------------------------------------------------------------

-- Init socketing and create a server socket on Port 5000
Socket.init()
server = Socket.createServerSocket(5000)

-- Create a debug console
cns = Console.new(TOP_SCREEN)
state = 0

while true do

-- Clear screen
Screen.refresh()
Screen.clear(TOP_SCREEN)

-- State 0: Looking for connections
if state == 0 then

-- Writing info on screen
Console.append(cns, "Looking for connections...")
Console.append(cns, "\nIP: "..Network.getIPAddress())
Console.show(cns)
Screen.flip()
Screen.waitVblankStart()
Console.clear(cns)

-- Searching for clients
client = Socket.accept(server)
if client == nil then
state = 0
else
state = 1
end

-- State 1: Connection estabilished, sending response to client
elseif state == 1 then

-- Writing info on screen
Console.append(cns, "Sending magic...\n")
Console.show(cns)
Screen.flip()
Screen.waitVblankStart()
Console.clear(cns)

-- Sending magic word expected from client to communicate
Socket.send(client, "YATA SENDER")
state = 2

-- Opening file to be received
new_theme = io.open("/tmp.zip",FCREATE)
offs = 0

-- State 2: File receiver
elseif state == 2 then

-- Writing info on screen
Console.append(cns, "Receiving file...\n")
Console.show(cns)
Screen.flip()
Screen.waitVblankStart()

-- File receiver on work with 256 bytes packets
block = Socket.receive(client, 256)
while string.len(block) > 0 do
io.write(new_theme, offs, block, string.len(block))
offs = offs + string.len(block)
block = Socket.receive(client, 256)
end

-- Closing sockets instances and file
io.close(new_theme)
Socket.close(server)
Socket.close(client)
Socket.term()

-- Writing info on screen
Console.clear(cns)
Console.append(cns, "File received\nPress START to exit.")
state = 3

else

-- Script finished
Console.show(cns)
Screen.flip()
Screen.waitVblankStart()
if Controls.check(Controls.read(), KEY_START) then
System.exit()
end

end

end

0 comments on commit 4b85381

Please sign in to comment.