B4J Tutorial [Server] Upload files from your B4A app to your B4J server over the internet

Status
Not open for further replies.
This example shows how to make your B4J server application accessible over the internet.

The implemented server is a simple file server that allows you to upload files from your B4A application to the server over the internet.

The following steps are required in order to make your server accessible over the internet:
  1. As the router (or computer) external IP address changes from time to time we need to use a service that maps a known address to our dynamic address. In this case I used a free service named DuckDNS. You should register with them and choose a unique domain for your server.
    They will also provide you with a token. In the Main module we use a timer to call their service (with this token) every 10 minutes. This updates the IP address if needed.
  2. Router setup
    • Port forwarding - Configure the router to forward all incoming requests on the given port to your computer address.
    • Static IP - On some routers port forwarding will only work with a specific local IP address. The solution is to configure the router to always provide your computer with the same address.
    • Search Google for instructions specific to your router.
  3. Firewall - You need to allow incoming connections on the specific port.
Tips:
- There are several points of failures in this configuration. In order to debug it, run the server and call the test handler. Start with calling it from the computer browser (127.0.0.1:54021/test). If there is an error then the server is not running properly.
Now try to call it with duckdns: yourdomain.duckdns.org:54021/test
If it works then the configuration is probably fine. You just need to test it from outside the local network.
If it doesn't work then find your external ip address and try to call it. If it doesn't work with the external ip address then your router is not configured correctly or the firewall is blocking the incoming connection.

- Don't worry. Eventually you will get it working :)

Now for the actual file server. The handler is quite simple. It supports two types of messages: text messages and files. The handler reads the data directly from the request input stream. The type parameter (from the query string) tells it whether to treat the data as a text message or as a file that needs to be saved.

One important point is that we need to call req.InputStream before calling req.GetParameter. Otherwise the complete stream will be internally read as the server looks for the parameter in the request body. This will not happen if we first call req.InputStream.

The Android application is quite simple. It just uses HttpUtils2 to send the file or text message.

upload_2014-1-28_13-9-44.png


In order to run it you need to update the link variable based on your domain name.

This tutorial opens the door to many interesting solutions. You can easily turn any PC into a backend server for your Android applications.
 

Attachments

  • FileClient-B4A.zip
    7.5 KB · Views: 4,158
  • FileServer-B4J.zip
    4.3 KB · Views: 4,408

TAK

Member
Licensed User
Hello, i try to save the massage in a file. But it doesnt work with "File.WriteString". How can i do it else?
 

TAK

Member
Licensed User
upload_2014-1-28_13-9-44-png.22299

"Hello from B4A app!!!" for example.
How can i write this string in a file?
I tried it with "File.WriteString" but i didnt work.
 

Luciano Boscolo

Member
Licensed User
Hello Erel,
in the FileServer side there's this in Process_Globals:
B4X:
Public filesFolder As String = "uploaded"

What do I need to write if I want to change to a different folder like the following?

B4X:
Public filesFolder As String = "c:\Remark_data\import"

Thank you
 

Philip Prins

Active Member
Licensed User
Hello Erel,

I want to use the audio streaming (walkie talkie) to send to a B4J server that sends this audio stream to all other clients connected.
Is this possible?

Regards
Philip
 

TomDuncan

Active Member
Licensed User
Can the client be written in b4J as well.
My app will use a B4J server and the clients will be Android, IOS and B4J.

Tom
 

TomDuncan

Active Member
Licensed User
Has anyone got some demo code for Sending an image file from a client and saving it on the server?

Tom
 

luke2012

Well-Known Member
Licensed User
Hi @Erel,
I understood the token system :) Only one question: this free service (DUCKDNS), in terms of reliability, is suitable only for dev & testing purpose or also for small env. production ?
 
Last edited:

sasidhar

Active Member
Licensed User
Very useful tutorial @Erel!
The only "limitation" about this solution regarding the configuration that enable the Server to be available over the internet.
I mean that the Server setup cannot be done by the final user (if the user doesn't have an internal IT person).

So we can distribute the client (android app) and the Server with the server app but we have to specify the above instructions to the final user.


Hi,

When i run fileserver.jar at server where i have static IP. I am getting Java error. please let me know why this issue coming in command line.


D:\New Folder\FileServer\Objects\Objects>java -jar fileserver.jar

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: anywheresoftware/b4a/
BA
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: anywheresoftware.b4a.BA
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more

thanks
sasidhar
 

sasidhar

Active Member
Licensed User
Thanks Erel..working well.. Very nice library. I am receiving both messages and Images to server.
How can i send server to Mobile from same server application. Is it possible or need to use some other other libraries?
Any examples similar.

thanks
sasidhar.M
 

haddad

Member
Licensed User
hi Erel

i get this error
2015-08-26 00:04:25.890:INFO::main: Logging initialized @527ms
2015-08-26 00:04:26.010:INFO:eek:ejs.Server:main: jetty-9.1.z-SNAPSHOT
2015-08-26 00:04:26.044:WARN:eek:ejh.MimeTypes:main: java.util.MissingResourceException: Can't find bundle for base name org/eclipse/jetty/http/encoding, locale fr_FR
2015-08-26 00:04:26.059:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@64616ca2{/,file:/C:/Users/HADDAD/Desktop/FileServer/Objects/www,AVAILABLE}
2015-08-26 00:04:26.064:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\Users\HADDAD\Desktop\FileServer\Objects\logs\b4j-2015_08_25.request.log
2015-08-26 00:04:26.072:WARN:eek:ejuc.AbstractLifeCycle:main: FAILED ServerConnector@17550481{HTTP/1.1}{0.0.0.0:32482}: java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.serverSocket(Net.java:420)
at sun.nio.ch.ServerSocketChannelImpl.<init>(ServerSocketChannelImpl.java:88)
at sun.nio.ch.SelectorProviderImpl.openServerSocketChannel(SelectorProviderImpl.java:56)
at java.nio.channels.ServerSocketChannel.open(ServerSocketChannel.java:108)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:276)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:218)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:337)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:151)
at b4j.example.main._appstart(main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
2015-08-26 00:04:26.073:WARN:eek:ejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.server.Server@735f7ae5: java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.serverSocket(Net.java:420)
at sun.nio.ch.ServerSocketChannelImpl.<init>(ServerSocketChannelImpl.java:88)
at sun.nio.ch.SelectorProviderImpl.openServerSocketChannel(SelectorProviderImpl.java:56)
at java.nio.channels.ServerSocketChannel.open(ServerSocketChannel.java:108)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:276)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:218)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:337)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:151)
at b4j.example.main._appstart(main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
main._appstart (java line: 63)
java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.serverSocket(Net.java:420)
at sun.nio.ch.ServerSocketChannelImpl.<init>(ServerSocketChannelImpl.java:88)
at sun.nio.ch.SelectorProviderImpl.openServerSocketChannel(SelectorProviderImpl.java:56)
at java.nio.channels.ServerSocketChannel.open(ServerSocketChannel.java:108)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:276)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:218)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:337)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:151)
at b4j.example.main._appstart(main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
main.main (java line: 29)
java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
Caused by: java.net.SocketException: Unrecognized Windows Sockets error: 87: socket
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.serverSocket(Net.java:420)
at sun.nio.ch.ServerSocketChannelImpl.<init>(ServerSocketChannelImpl.java:88)
at sun.nio.ch.SelectorProviderImpl.openServerSocketChannel(SelectorProviderImpl.java:56)
at java.nio.channels.ServerSocketChannel.open(ServerSocketChannel.java:108)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:276)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:218)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:337)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:151)
at b4j.example.main._appstart(main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
... 2 more

why plz
 
Status
Not open for further replies.
Top