All pastes #511669 Raw Edit

Anonymous

public text v1 · immutable
#511669 ·published 2007-05-26 15:56 UTC
rendered paste body
--Load the socket library
require("socket")

--Set version to check with client
VERSION = "0.1"

--Create our client object
listening = socket.tcp()

--Set the client to listen
assert(listening:bind("127.0.0.1", 2222))
assert(listening:listen())

--Create connections table
clients = {}

--Add our initial connection to the table
table.insert(clients,listening)

function onClientConnect(client)
	connected:send("HELLO")
	connected:send("VERSION "..VERSION)
	print("Client connected.")
end

--Loop forever
while true do

	local reading,writing,err = socket.select(clients,nil)
	
	if reading[listening] then
		local connected = listening:accept()
		table.insert(clients,connected)
		onClientConnect(connected)
	else
		for k,v in pairs(reading) do
			local message,err = k:receieve("*l")
			if err == nil then
				print(message)
			end
		end
	end
	
end

--Parse command from a client. (Got this ready for later)
function ParseCommand(command)

	if command == "HELLO" then return "HELLO" end
	
end