--****************************************************************************** --* * --* File: Socket_02.lua Revision: 1.0 * --* * --* Contents: a first experiment with TCP streams * --* * --* Creation: 09.03.2002 Last Modification: 09.03.2002 * --* * --* Platform: IBM-compatible PC running Windows 98SE * --* * --* Environment: Lua 4.0, TkLua 4.0, LuaSocket 1.4 * --* * --* Author: Andreas Rozek Phone: ++49 (711) 6770682 * --* Kirschblütenweg 15 Fax: - * --* D-70569 Stuttgart EMail: Andreas.Rozek@T-Online.De * --* Germany * --* * --* URL: http://www.Andreas-Rozek.de/ * --* * --* Copyright: the software is published under the "GNU Lesser General Pub- * --* lic License" (see "http://www.fsf.org/copyleft/lesser.html" * --* for additional information) * --* * --* Comments: (none) * --* * --****************************************************************************** dofile("Lua_02_Lib.lua"); -- provides extended versions of write, print, etc. print(); print("Socket_02 - a first experiment with TCP streams"); print(); print("creating server socket..."); local CmdSocket,ErrMsg = bind("*",0); assert(CmdSocket, ErrMsg); CmdAddr,CmdPort = CmdSocket:getsockname(); print("done (Address: ",CmdAddr,", Port: ",CmdPort,")"); print(); print("creating receiver socket (contacting server)..."); local InSocket,ErrMsg = connect("127.0.0.1",CmdPort); assert(InSocket, ErrMsg); InAddr,InPort = InSocket:getsockname(); print("done (Address: ",InAddr,", Port: ",InPort,")"); print(); print("creating sender socket (accepting ConnReq from receiver)..."); local OutSocket,ErrMsg = CmdSocket:accept(); assert(OutSocket, ErrMsg); OutAddr,OutPort = OutSocket:getsockname(); print("done (Address: ",OutAddr,", Port: ",OutPort,")"); print(); print("sending message..."); ErrMsg = OutSocket:send("Hello\n"); -- '\n' acts a message terminator assert(ErrMsg == nil, ErrMsg); print("done"); print(); print("receiving message..."); InSocket:timeout(4); local Message,ErrMsg = InSocket:receive(); assert(ErrMsg == nil,ErrMsg); -- since 'Message' may still contain some text print("done (got \""..Message.."\")"); exit();