Android Question Correct URL to connect to FTP server

crt

Member
Licensed User
Longtime User
Hi, Usually I try to do searches to get my answers instead of asking questions and so far I have been getting by, but now I need to ask a question. I'm trying to connect to the FTP server that is in this thread https://www.b4x.com/android/forum/t...plemented-with-socket-and-asyncstreams.74320/
Using a visual basic desktop app. Basically what I am trying to do is copy a database file from a device to the PC then sync with it from a desktop app and then copy it back to the device. I figured FTP is an easy and reliable way to do this. I have tried different URL syntax but it is still unable to connect.
I figured this syntax would work ftp://192.168.2.1:15162/ that is, using the URL and port number but everything I tried doesn't work. I've set passive mode to true and false but it none had any effect.

Here is the code I'm using in visual basic

B4X:
Private Sub FtpUploadFile(ByVal filetoupload As String, ByVal ftpuri As String, ByVal ftpusername As String, ByVal ftppassword As String)
        ' Create a web request that will be used to talk with the server and set the request method to upload a file by ftp.
        Dim ftpRequest As FtpWebRequest = DirectCast(WebRequest.Create(ftpuri), FtpWebRequest)
        '(CType(WebRequest.Create(ftpuri), FtpWebRequest)
        Try
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
            ftpusername = "Test"
            ftppassword = "test"
            ftpRequest.UsePassive = True
            ' Confirm the Network credentials based on the user name and password passed in.
            ftpRequest.Credentials = New NetworkCredential(ftpusername, ftppassword)

            ' Read into a Byte array the contents of the file to be uploaded
            Dim bytes() As Byte = System.IO.File.ReadAllBytes(filetoupload)

            ' Transfer the byte array contents into the request stream, write and then close when done.
            ftpRequest.ContentLength = bytes.Length
            Using UploadStream As Stream = ftpRequest.GetRequestStream()
                UploadStream.Write(bytes, 0, bytes.Length)
                UploadStream.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

        MessageBox.Show("Process Complete")
    End Sub



    Private Sub FTPDownloadFile(ByVal downloadpath As String, ByVal ftpuri As String, ByVal ftpusername As String, ByVal ftppassword As String)
        'Create a WebClient.
        Dim request As New WebClient()
        ftpusername = "Test"
        ftppassword = "test"
        ' Confirm the Network credentials based on the user name and password passed in.
        request.Credentials = New NetworkCredential(ftpusername, ftppassword)

        'Read the file data into a Byte array
        Dim bytes() As Byte = request.DownloadData(ftpuri)

        Try
            '  Create a FileStream to read the file into
            Dim DownloadStream As FileStream = IO.File.Create(downloadpath)
            '  Stream this data into the file
            DownloadStream.Write(bytes, 0, bytes.Length)
            '  Close the FileStream
            DownloadStream.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

        MessageBox.Show("Process Complete")

    End Sub

I'm using a text box to enter the url and an openfiledialog for the file to send and then passing the arguments to the subroutine.

Any help would be appreciated.
 

eps

Expert
Licensed User
Longtime User
I'm not sure about using FTP in this way - as I'm sure you're aware it stands for File Transfer Protocol.. So literally transferring files from one location to another.

The ideal scenario - but you might not have the capabilities (in terms of servers available) or experience would be to have a central database and access that or trigger a refresh.

But anyway, that doesn't directly solve your issue here.

Where's the B4A code? It's only available in passive mode - take care with case of directories and files, etc.. as this will fail.

Logs from the Server? Are you even initiating a connection for instance?
 
Upvote 0

crt

Member
Licensed User
Longtime User
Hi, the b4a code I'm using is the ftp server example in the link I provided and I would think the ftp connection would be initiated by
FtpWebRequest in the visual basic app. On some url syntax I try, I get an error saying uri format is not correct and others it just says cannot connect to ftp server. Since I want to transfer a file from the device to a PC, I would think FTP would be a logical solution.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
And you HAVE A FTP SERVER SOFTWARE running on your PC??????
 
Upvote 0

crt

Member
Licensed User
Longtime User
And you HAVE A FTP SERVER SOFTWARE running on your PC??????
No, I am using the desktop app as a client in visual basic on the PC. And the b4a code running on the device is the server. That way it is the Windows app on the PC that controls the uploads and downloads from and to the device. I figure the answer is probably very simple, I'm just missing something in the visual basic code.
 
Upvote 0

crt

Member
Licensed User
Longtime User
Upvote 0

crt

Member
Licensed User
Longtime User
Sorry, it is a b4a server... You are right. I did not remember it exists. :-/
I never used it to build a server on Android side....
I always use the device as client.

Sorry, i can´t help on this any further. I dont know VB.

Thanks, I appreciate your reply, I hope to figure it out eventually, or someone to chime in with a clue.

Thanks again.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Sorry, so the problem you have is in the VB code?

Have you tried using B4J there's an example in the thread there.
 
Upvote 0

crt

Member
Licensed User
Longtime User
Sorry, so the problem you have is in the VB code?

Have you tried using B4J there's an example in the thread there.
Yes it seems to be the Visual Basic side. I haven't tried the b4j code, or filezilla. The b4a code on the device is an example and is unmodified so I have to assume there is nothing wrong with it. So the problem is how I'm using the visual basic code. I guess stack overflow might be a better forum to ask a visual basic question since the problem is with vb. I figured there must be a basic syntax used in ftp to connect to a server.
 
Upvote 0

crt

Member
Licensed User
Longtime User
Well, for anyone coming across this problem, it seems there is a bug of sorts in visual basic .net the System.net.FtpWebresponse library. I am able to connect to the b4x FTP server app, but when it sends the response "227" that it is entering passive mode, Visual Basic .Net sees this as an error and terminates. There could be other problems causing it like a firewall or something, but I ruled all of that out. The way around this is to set Passive mode to false, but the ftp server app example for b4a doesn't support passive mode being false.
Best thing to do is learn Java!
Hope this helps somebody
 
Upvote 0

crt

Member
Licensed User
Longtime User
Best thing to do is learn B4J. If you know B4A then you already know B4J.
Wow, thanks for that, I figured B4J was a paid for product so I never even looked at it. Thats seems to be the way to go, I've got alot of porting to do:)
But in the mean time, I've been doing some more searching about my query. I tested the VB.net program on other ftp servers with no problem in passive mode. So now I have to place the blame on both vb.net and the b4a ftpserver example. It seems the response the server is sending isn't exactly what a ftp client is looking for. Commercial programs like filezilla and the like, seem to be able to ignore this error. I know its a bug in vb.net but there seems to be something not 100% with the ftpserver code in b4a too.

Just my observation
 
Upvote 0

crt

Member
Licensed User
Longtime User
B4J is incredibly easy to use and free! That's why I mentioned it some posts ago..
Yeah, I started learning Java years ago when I was modding and compiling AOSP. I really wish I stuck with it. I guess using visual studio kept me from venturing into much of anything else for programming apps for Windows. I did dabble in Delphi (Pascal) years ago too but forgot most of it now, another regret I have.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
It's completely akin to B4X. I had a Windows App I wanted to develop and I got it completed in B4J in an evening. :)
 
Upvote 0

crt

Member
Licensed User
Longtime User
It's completely akin to B4X. I had a Windows App I wanted to develop and I got it completed in B4J in an evening. :)

It is very cool, I can't believe I haven't checked it out before now. One question, when I use the wysiwyg designer, I can't move any of the controls around, I can only move them in the abstract designer, is it suppose to be like that or am I doing something wrong?
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Hmm.. I don't know - I think you can resize the window using that but that might be about it. It looks like it's more to show what it will look like when it's executed.

The tutorial video about the UI designer closes the WYSIWYG designer window almost immediately!

I'm sure someone else will be along who uses it more than I do. I've got quite an old version installed at the moment.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
One question, when I use the wysiwyg designer, I can't move any of the controls around, I can only move them in the abstract designer, is it suppose to be like that or am I doing something wrong?
Yes. You can only move them in the abstract designer. Note that you should have started a new thread for this question.
 
Upvote 0
Top