0

I'm trying to understand the socket class and i'm using the following example to implement a server sample

local server = assert(socket.bind("*", 0))

-- find out which port the OS chose for us
local ip, port = server:getsockname()

-- print a message informing what's up
print("Please telnet to localhost on IP [" ..ip.. "] and port [" .. port .. "]")
print("After connecting, you have 10s to enter a line to be echoed")

-- loop forever waiting for clients
while true do
-- wait for a connection from any client
local client = server:accept()

-- make sure we don't block waiting for this client's line
client:settimeout(10)

-- receive the line
local line, err = client:receive()

-- if there was no error, send it back to the client
if not err then
    client:send(line .. "\n")
end

-- done with client, close the object
client:close()
end

But now the question is, how can I telnet for example the address localhost:8080 via lua?

EDIT: I forgot to tell something, I don´t even can telnet on cmd. When I type the command:

telnet ip port

it always says "connection lost" after I send a message. What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
briba
  • 2,857
  • 2
  • 31
  • 59

2 Answers2

2

First, follow the instructions from here to enable telnet in Windows 7:

  1. Go to Control Panel
  2. Find Turn Windows features on or off under Programs (depending on layout)
  3. Find Telnet client and enable it.

Once you've done that, it should work as expected.

  • I did this to. Actually when i type the telnet command, it connects, but when i send a message ("hello" for example), my connection is lost and my lua program can`t receive the message. I dont know why... – briba Apr 10 '13 at 14:57
  • Telnet requires both a client and a server. –  Apr 10 '13 at 16:02
  • My client and server is active, but i still get the same message dont know why... – briba Apr 10 '13 at 16:50
  • My telnet client and server are active. I can connect via telnet on prompt command, but when i try to send a value ("hello" for example"), the connection is lost. – briba Apr 13 '13 at 17:17
  • And i don´t know how to telnet (like a client code) via lua. Can anyone help me with this? Thanks! – briba Apr 13 '13 at 17:18
1

Done!

local socket = require("socket")

local server = socket.connect(ip, port)

local ok, err = server:send("RETURN\n")
if (err ~= nil) then
    print (err)
else
    while true do
        s, status, partial = server:receive(1024)
        print(s or partial)

        if (status == "closed") then
            break
        end
    end
end

server:close()
briba
  • 2,857
  • 2
  • 31
  • 59