luasocket/samples/talker.lua

29 lines
769 B
Lua
Raw Normal View History

-----------------------------------------------------------------------------
-- TCP sample: Little program to send text lines to a given host/port
-- LuaSocket sample files
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
2001-01-25 22:57:07 +01:00
host = host or "localhost"
port = port or 8080
2000-12-29 23:15:09 +01:00
if arg then
host = arg[1] or host
port = arg[2] or port
end
print("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
2003-05-25 03:54:13 +02:00
c, e = socket.connect(host, port)
2000-12-29 23:15:09 +01:00
if not c then
print(e)
2003-05-25 03:54:13 +02:00
os.exit()
2000-12-29 23:15:09 +01:00
end
print("Connected! Please type stuff (empty line to stop):")
2003-05-25 03:54:13 +02:00
l = io.read()
2000-12-29 23:15:09 +01:00
while l and l ~= "" and not e do
2003-05-25 03:54:13 +02:00
t, e = c:send(l, "\n")
2000-12-29 23:15:09 +01:00
if e then
print(e)
2003-05-25 03:54:13 +02:00
os.exit()
2000-12-29 23:15:09 +01:00
end
2003-05-25 03:54:13 +02:00
l = io.read()
2000-12-29 23:15:09 +01:00
end