I shall explain these programs line by line.

Before that in case any of you wondered this is how you would run it.

$ lua echosrvr.lua

In another terminal

$ lua echoclnt.lua
hi

will get printed on both sides as the text will be echoed back thro' the UDP
socket.

I hope you guys got that far at least if not any farther. ;)

Let us dissect the program presently.

My comments begin with a <comment> tag.


1) echoclnt.lua
-- -----------------------------------------------------------------------------
-- UDP sample: echo protocol client
-- LuaSocket sample files
-- Author: Diego Nehab
-- RCS ID: $Id: echoclnt.lua,v 1.10 2005/01/02 22:44:00 diego Exp $
-----------------------------------------------------------------------------

<comment> All comments in lua start with '--'

local socket = require("socket")

<comment> we import the 'socket' module similar to perl 'use' or python 'import'

host = host or "localhost"
port = port or 1234
if arg then
   host = arg[1] or host
   port = arg[2] or port
end

<comment> The above lines are reasonably straight forward. arg[1] to
arg[n] stand for
 <comment> command line arguments. Also note if then ... end. You also have
 <comment> while do ... end and for do ... end


host = socket.dns.toip(host)
<comment> You get IP from DNS. You can do print(host) and check it
here if you want


udp = assert(socket.udp())
assert(udp:setpeername(host, port))

<comment> You see assert() in action here. If the value is empty
program will exit I think.
<comment> You can also do udp = socket.udp(). You are opening a UDP socket.
<comment> setpeername() is necessary I think in lua. Not sure

print("Using remote host '" ..host.. "' and port " .. port .. "...")

<comment> print() function like C printf or perl print or python
print. Just like python you also
<comment> an interactive interpreter. Just type lua. You also have an
alias for print called '='.
<comment> For example: lua>=3*5 will give 15

while 1 do
       line = io.read()
       if not line or line == "" then os.exit() end
       assert(udp:send(line))
       dgram = assert(udp:receive())
       print(dgram)
end

<comment> While loop in action. io.read() and io.write() will read and
write from/to STDIN/STDOUT.
<comment> Exit on empty line and invoke the send() function of udp table.
<comment>  Then receive...

Now let us move on to the server.

Just copy paste this and use this server with it:

2)echosrvr.lua
-----------------------------------------------------------------------------
-- UDP sample: echo protocol server
-- LuaSocket sample files
-- Author: Diego Nehab
-- RCS ID: $Id: echosrvr.lua,v 1.12 2005/11/22 08:33:29 diego Exp $
-----------------------------------------------------------------------------
local socket = require("socket")
host = host or "127.0.0.1"
port = port or 1234
if arg then
   host = arg[1] or host
   port = arg[2] or port
end

<same as above>

print("Binding to host '" ..host.. "' and port " ..port.. "...")
udp = assert(socket.udp())
assert(udp:setsockname(host, port))
assert(udp:settimeout(5))

<comment> In the server you have to bind. You also set a timeout

ip, port = udp:getsockname()

<comment> simultaneous assignment of two values like in perl or python

assert(ip, port)

<comment> Ensuing that both values are defined and usable

print("Waiting packets on " .. ip .. ":" .. port .. "...")

<comment> You give arguments to print() or io.write() using ..
operator. Like %s in C and
<comment> % in python and . in perl.

while 1 do
       dgram, ip, port = udp:receivefrom()
       if dgram then
               print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
               udp:sendto(dgram, ip, port)
       else
       print(ip)
       end
end

<comment> infinite loop. Please note the end placements to signify if
and while closing points

Please ask in case of doubts. This should get you started. Tomorrow we
will examine a lua
 sample to print thumbnails of images. With that lua tutorial will end.;)

If I get time to learn this language properly I shall organize a one
day programme on lua within 2 months.

If you want to sponsor a hall, please mail me or Bharathi in private.

-Girish
-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com
_______________________________________________
To unsubscribe, email [email protected] with 
"unsubscribe <password> <address>"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to