Left here for archival purposes.

User avatar
By ThomasW
#6787
picpic020960 wrote:also why c:on("connection" ... event ?

For client connections:
Code: Select allconn=net.createConnection(net.TCP, 0)
-- setup callbacks:
conn:on("sent", function(sk)
    print("request sent")
end)
conn:on("disconnection", function(sk)
    print("all done")
end)
conn:on("receive", function(sk, payload)
    print("received:")
    print(payload)
    sk:close()
end)
conn:on("connection", function(sk)
    print("connected!")
    sk:send("GET /whatever?data=something HTTP/1.0\r\n\r\n")
end)
-- initiate the connection:
conn:connect(80,"10.10.1.119")

Putting the request inside the "connection" event makes sure the send() doesn't occur before the actual connection is established. There are some examples out there doing the send() immediately after the connect() but this is wrong and will occasionally fail on a slightly slower network or when the connect() uses a hostname instead of an IP-address.

picpic020960 wrote:what is a 'not initial connection' ?

Good question :) 'initial connection' was probably not the best wording... Maybe "handle the start of the conversation with the client". If you would write a SMTP-Server, this would be the place to send the "220 xxx.yyy.zzz" greeting.

Thomas