HELP
Posted: Sun Mar 27, 2011 4:16 am
I've been developing a game, and I have the game 'server' set up, but there's one thing I forgot : How do you use the Lua socket library. Anyone help!
amdintel32 wrote:How do you use the Lua socket library.
Code: Select all
require "socket"
Isn't itRobin wrote:amdintel32 wrote:How do you use the Lua socket library.Also see the luasocket documentation for how to go from there.Code: Select all
require "luasocket"
If you still have questions, feel free to ask them here. But can you be a bit more specific about what the problem is?
Code: Select all
require 'socket'
Code: Select all
function network_init(ip,port)
TCP_sock = socket.tcp()
TCP_sock:settimeout(7) --Wait up to 7 seconds to connect.
if not TCP_sock:connect(ip,port) then
error("Failed to connect =(")
end
TCP_sock:settimeout(0) --Don't wait on further calls (so we can use the socket for nonblocking I/O).
end
Code: Select all
local leftovers = ""
function flush_socket()
local junk,err,data = TCP_sock:receive('*a')
-- lol, if it returned successfully then that's bad!
if not err then
error("the connection closed unexpectedly")
end
leftovers = leftovers..data
end
local type_to_length = {G=1, H=1, N=1, P=121, O=121, I=23}
function get_message()
if string.len(leftovers) == 0 then
return nil
end
local typ = string.sub(leftovers,1,1)
local len = type_to_length[typ]
if len > string.len(leftovers) then
return nil
end
local ret = string.sub(leftovers,2,type_to_length[typ])
leftovers = string.sub(leftovers,type_to_length[typ]+1)
return typ, ret
end
local process_message = {
G=function(s) ask_for_panels("000000") end,
H=function(s) end,
N=function(s) error("Server rejected connection due to protocol missmatch.") end,
P=function(s) P1.panel_buffer = P1.panel_buffer..s end,
O=function(s) P2.panel_buffer = P2.panel_buffer..s end,
I=function(s) P2.input_buffer = P2.input_buffer..s end}
function do_messages()
flush_socket()
while true do
local typ, data = get_message()
if typ then
process_message[typ](data)
else
break
end
end
end
Code: Select all
TCP_sock::send(some_string)