Next Topic

Previous Topic

Book Contents

Sample Script: TLuaSocket

--Construct a new socket object
socket = TLuaSocket()
iPortNumber = 8080

--Open a TCP socket
iRet = socket:OpenTCP(iPortNumber)

--If OpenTCP returned a 0 (boolean FALSE) then the open failed
if iRet==0 then

	print("Cannot open port "..iPortNumber.." Error code:"..GetLastError())

else
	
	--Read some data (max 1024 bytes) from the socket
	iReadSize = 1024
	data = socket:Read(iReadSize)
	
	--Print the content
	if iReadSize > 0 then
		print("Data received from server:\n\n")
		print(data)
	else
		print("No data received from server")
	end

end
socket:Close()