Created
August 3, 2015 12:07
-
-
Save edran/d50584f5088b2e5c8de2 to your computer and use it in GitHub Desktop.
Small buffer receiving set of functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'struct' | |
-- TODO: make class | |
print "Initializing server..." | |
server = socket.tcp() | |
server:bind('*',12312) | |
server:listen(32) | |
client = server:accept() | |
print "Server has been initialized!" | |
buffer = "" | |
function get_size() | |
local header_length = 4 | |
-- local buffer = "" | |
while string.len(buffer) < header_length do | |
print("receiving size!") | |
buffer = buffer .. client:receive(4) | |
end | |
local header = string.sub(buffer, 1, 5) | |
buffer = string.sub(buffer, 5) | |
buffer_length = struct.unpack('>I4', header) | |
-- buffer = string.sub(buffer, header_length) | |
-- print(buffer) | |
return buffer_length | |
end | |
function get_packet() | |
buffer_length = get_size() | |
print("Size: ", buffer_length) | |
-- buffer = "" | |
while string.len(buffer) < buffer_length do | |
buffer = buffer .. client:receive(buffer_length) | |
end | |
local s = string.sub(buffer, 1, buffer_length+1) | |
buffer = string.sub(buffer, buffer_length+1) | |
return s | |
end | |
function get_image() | |
image_width = get_size() | |
image_height = get_size() | |
last_size = get_size() | |
print("Size: ", last_size) | |
-- buffer = "" | |
while string.len(buffer) < last_size do | |
buffer = buffer .. client:receive(last_size) | |
end | |
local s = string.sub(buffer, 1, last_size+1) | |
buffer = string.sub(buffer, buffer_length+1) | |
return s | |
end | |
while true do | |
local packet = get_packet() | |
print(string.sub(packet, 1, 10)) | |
local image = get_image() | |
print(string.sub(image, 1, 10)) | |
end | |
client:close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment