HELP
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Sun Mar 27, 2011 4:12 am
HELP
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!
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: HELP
amdintel32 wrote:How do you use the Lua socket library.
Code: Select all
require "socket"
If you still have questions, feel free to ask them here. But can you be a bit more specific about what the problem is?
Last edited by Robin on Sun Mar 27, 2011 8:44 am, edited 1 time in total.
Help us help you: attach a .love.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: HELP
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'
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: HELP
D'oh. You're absolutely right. *goes on to modify his post to try and avoid further confusion*
Help us help you: attach a .love.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
- sharpobject
- Prole
- Posts: 44
- Joined: Fri Mar 18, 2011 2:32 pm
- Location: California
Re: HELP
I'm using it like this:
As soon as I know I want to use a socket I'll want to call network_init with the IP and port of the server.
Then, every frame I'll call do_messages. flush_socket reads data from the socket, appends it to a buffer, and errors if the connection has closed. After that, I'll read messages from the buffer until I run out. My game uses an extremely primitive RPC system, so each message is a one-character message type followed by an amount of data determined by the type of message. Each message type has its own function to process the data in the message.
Making a call to the server is much simpler:
As soon as I know I want to use a socket I'll want to call network_init with the IP and port of the server.
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)
Who is online
Users browsing this forum: Google [Bot] and 1 guest