Ir para conteúdo
Entre para seguir isso  
Mock

Uploader e downloader fail

Recommended Posts

Mock    32
Mock

nome auto explicativo:

uploader

require('socket')
require('ex')
SOCK = socket.bind('*',5721)

user = {}

function getUserCNT()
local nc = 0
for _,_ in pairs(user) do
	nc = nc+1
end
return nc
end

n = 0
while true do
local t = os.clock()
SOCK:settimeout(0)
local rsa = SOCK:accept()
if rsa then
	n = n+1
	local ip = rsa:getpeername()
	user[n] = {rsa,req='',ip=ip}
	print(ip,n)
	user[n][1]:send('HI,'..ip..',YOU,'..n..'/'..getUserCNT()..'\n')
end
for id,b in pairs(user) do
	b[1]:settimeout(0)
	local data,f,dat_ = b[1]:receive()
	if f == 'closed' then
		print(id,'close')
		user[id] = nil;
		collectgarbage()
		break
	end
	if b.download then
		if data then
			if data:match('S(%d+)') then
				local p = tonumber(data:match('S(%d+)'))
				if p ~= b.size then
					b[1]:send(b.file:sub(p,b.size))
					b.pos = p
				else
					b[1]:send('MSGDone\n')
					b[1]:close()
				end
			end
		end
	else
		if data then
			data = data..(dat_ or '')
			if data:match('D(.+)') then
				local fil = data:match('D(.+)')
				local file = io.open('dlc/'..fil,'rb')
				if file then
					b.file = file:read(-1)
					b.pos = 0;
					b.download = true;
					b.size = #b.file;
					b[1]:send('#'..#b.file..'\n')
				else
					b[1]:send('MSGFile not found\n')
				end
			end
		end
	end
end
if t-os.clock() <= 0.009 then
	os.sleep(0.01)
end

end

downloader

require('socket')
function donwload(ip,file)
local old = 0
local data = {log=''}
local s,err = socket.connect(ip,8090)
if not s then
	data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' '..err..' [1] on '..ip..'.\n'
	return false,err,1,data
end
data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' Connected at '..ip..'.\n'
s:settimeout(10)
local d,err = s:receive()
print(1,d)
if not d then
    data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' '..err..' [2] on '..ip..'.\n'
	s:close()
	return false,err,2,data
end
data.hail,data.ip,data.me,data.online = d:match('(.-),(.-),YOU,(%d+)/(%d+)')
if not data.hail then
	s:close()
    data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' protocol refused [3] on '..ip..'.\n'
	return false,'protocol refused',3,data
end
data.log = data.log..os.date("[%d %B %Y %X]",os.time())..string.format('Hail: %s, My ip: %s, Mid: %d Users online: %s.\n',data.hail,data.ip,data.me,data.online)
s:send('D'..file..'\n')
s:settimeout(20)
local d,err = s:receive()
if not d then
	s:close()
    data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' '..err..' [4] on '..ip..'.\n'
	return false,err,4,data
end
if d:match('#(%d+)') then
	data.size = tonumber(d:match('#(%d+)'))
	data.file = io.open('Download/'..file,'wb')
	data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' requested file '..file..' size is '..d..' bytes.\n'
	data.file = io.open('Download/'..file,'wb')
	data.now = 0;
	if not data.file then
		s:close()
		data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' Cannot open the file '..file..' [5] on '..ip..'.\n'
		return false,'Cannot open the file',5,data
	end
	s:send('S'..data.now..'\n')
	while true do
		s:settimeout(2)
		local d,err = s:receive()
		if err == 'timeout' then
			s:send('S'..data.now..'\n')
			print('-kd')
		end
		if err == 'closed' then
			s:close()
			data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' Closed ('..data.now..' Bytes) [6] on '..ip..'.\n'
			return false,'Closed ('..data.now..' Bytes)',6,data
		end
		if d then

			data.now = data.now+string.len(d)
			data.file:write(d)
			local percent = math.floor(((data.now/data.size)*100)*10)/10
			if percent ~= old then
				local topr = data.now..'/'..data.size..' ('..percent..'%) This pack: '..string.len(d)
				data.log = data.log..os.date("[%d %B %Y %X]",os.time())..topr
				print(topr)
				old = percent
			end
			s:send('S'..data.now..'\n')
			if data.now == data.size then
				data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' Download finished ('..data.now..'Bytes).\n'
				data.file:close()
				collectgarbage()
				s:close()
				return true,s:receive(),-1,data
			end
		end
	end
else
	local msg = d:match('MSG(.+)')
	s:close()
	data.log = data.log..os.date("[%d %B %Y %X]",os.time())..' Server message: '..msg..' [8] on '..ip..'.\n'
	return false,msg,8,data
end
end
local d,msg,err,data = donwload('69.162.100.200','omg.mp3')
print(d,msg,err)
print(data.log)
io.read()

 

 

dps q eu fiz isso descobri que n rola usar lua socket p fazer uploade de arquivos. MUITOS PACOTES SE PERDEM "-"

 

ou eu q fiz merda mesmo. em fim ele usa um protcolo proprio.

Compartilhar este post


Link para o post
Compartilhar em outros sites
Fletch    0
Fletch

tem nada melhor pra fazer download e upload atualmente que a web (php, asp) qualquer um poem alguma coisa pa baxa nu sitizin dele e n precisa entende bosta nenhuma de programação hehe

Compartilhar este post


Link para o post
Compartilhar em outros sites
Mock    32
Mock
tem nada melhor pra fazer download e upload atualmente que a web (php, asp) qualquer um poem alguma coisa pa baxa nu sitizin dele e n precisa entende bosta nenhuma de programação hehe

 

pois é, php e asp ja tem libs prontas, libs especificas para isso. Lua não. Alem disso lua socket é bugado...

Compartilhar este post


Link para o post
Compartilhar em outros sites
Visitante
Este tópico está impedido de receber novos posts.
Entre para seguir isso  

  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×