Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Help with sockets... — Gideros Forum

Help with sockets...

chuz0chuz0 Member
edited November 2015 in General questions
Hi,
I've been struggling with sockets for the past few days.
The app I'm making is a survey; You answer questions and each result is stored in a table. At the end of the survey I iterate through the elements of the table and append them to a string :
local str = ""
     for i=1,#answers do
          str = str..answers[i]..","
     end
So, after the survey I end up with a .csv string. What I want then, is to append that string to a .txt file that is stored remotely in a FTP server.
And now what I've tried with the sockets....
        local socketftp = require("socket.ftp")	
	local hostftp = "domain.com"
	local client = socketftp.open(hostftp,21,create)
	client:greet()
	client:login("username","password")
        local fi = io.open("|D|testftp.txt","wb")
        client:send(hostftp,fi)
Ok, so I believe it does the login correctly (if I enter a wrong username/password it times out and crashes), but I don't really know how to use the client:send() command...

Any help would be much appreciated.

Kind Regards.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited November 2015
    I think this should be as easy as:
    local ftp = require("ftp")
    ftp.put("<a href="ftp://username:password@domain.com/filename.txt"" rel="nofollow">ftp://username:password@domain.com/filename.txt"</a>, content_to_save_in_file)
    of course you need to include ftp.lua from All Plugins/luasocket/source

    But I have no tried it myself, its what I get from examples :)
  • Thanks @ar2sawseen, I've tried, but it gives this error on the 2nd line:
    attempt to index local 'ftp' (a boolean value)

    Also... where can I find examples of the ftp for Gideros ?

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @chuz0 Gideros uses unmodified lua sockets. So all examples of lua sockets should work on Gideros.
    The example I was referring to is:
    http://w3.impa.br/~diego/software/luasocket/ftp.html

    and I tried it myself now and needed to add more file from luasocket and make ftp code dependent on url and tp and the correct code seems to be:
    local ftp = require("socket.ftp")
    ftp.put("<a href="ftp://username:password@domain.com/filename.txt&quot;" rel="nofollow">ftp://username:password@domain.com/filename.txt&quot;</a>, content_to_save_in_file)
  • chuz0chuz0 Member
    edited November 2015
    Thank you again @ar2rsawseen, I've finally understood how it works.

    This code will create a file in the ftp server with the text specified. If the file exists, it will erase its contents.
    local ftp = require("socket.ftp")
    ftp.put("<a href="ftp://username:password@domain.com/filename.txt"" rel="nofollow">ftp://username:password@domain.com/filename.txt"</a>, content_to_save_in_file)
    If what you want to do is to upload the contents of a file and append it to a file in the ftp server (my case), this is the code I've used :
            local socketftp = require("socket.ftp")
    	local ltn12 = require("ltn12")
     
    	f,e = socketftp.put{
    		host = "domain.com", 
    		user = "username",
    		password = "password",
    		command = "appe",
    		argument = "/public_html/target.txt", //public_html is just the folder where target.txt is.
    		source = ltn12.source.file(io.open("|D|source.txt", "r"))
             }
    Also, if you want to run this from Android, you have to create a folder named 'socket' in your project and put there tp.lua, url.lua and ftp.lua

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • Yes, only in desktop you can reference files from Gideros installation folder
    in other platforms, you would need to copy files into your project and then use them from there

    Glad it worked for you ;)
Sign In or Register to comment.