B4AServer - Simple way to connect Android devices to a desktop server

Erel

B4X founder
Staff member
Licensed User
Longtime User
Edit: B4AServer is now available here: http://www.b4x.com/forum/basic4andr...nt-enterprise-house-android-applications.html

This is a proof of concept of a mini-project which will soon be available.

The purpose of this project is to make it simpler to connect one or more devices to a desktop that is acting as a server.
The first version will allow the devices to download files and upload files to the server and to run shell commands on the server (returning the result to the device).

B4AServer will simplify the development of in-house applications and make it easier to integrate the mobile devices with existing applications and databases.
For example you can create a simple desktop program that accesses your local database and returns the result to the device. The device will then connect to the server, the server will run the custom program and will return the result to the device.
Or you can upload a file and the call a program that will print this file.

It may be a bit surprising but it is pretty difficult to connect to a regular desktop running Windows due to firewall issues. This makes the solution a bit complicated. I've tried to hide the complexity in the service module and I hope that the solution will be simple enough.

An example code that downloads and uploads files looks like
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        'Change test1 to the server name you choose!
        B4AServer.Initialize("http://66.84.14.57/android/b4a_server.php", "test1", "Main")
    End If
    btnSendFile.Initialize("btnSendFile")
    btnSendFile.Text = "Send File"
    btnReceiveFile.Initialize("btnReceiveFile")
    btnReceiveFile.Text = "Receive File"
    Activity.AddView(btnSendFile, 10dip, 10dip, 300dip, 100dip)
    Activity.AddView(btnReceiveFile, 10dip, 150dip, 300dip, 100dip)
End Sub


Sub TaskComplete(Result As TaskResult)
    Log(Result.Id & " done. Success=" & Result.Success & ", msg=" & Result.Message)
    If Result.Id = imgTask AND Result.Success = True Then 
        Activity.SetBackgroundImage( _ 
            LoadBitmapSample(B4AServer.DownloadFilesFolder, Result.Id, Activity.Width, Activity.Height))
    End If
End Sub

Sub btnSendFile_Click
    B4AServer.SendFile(File.DirRootExternal, "somefile.dat")
End Sub
Sub btnReceiveFile_Click
    imgTask = B4AServer.ReceiveFile("logo.jpg")
End Sub
B4AServer is made of three components:
- "Board" server - An online site running a php script. The desktop server polls the board server every several seconds and checks if there are any waiting devices that want to connect.
- Desktop server - This is a java server.
- Android devices - When a device wants to connect (assuming that it is not already connected) it calls the board server. The desktop server will then see the waiting device(s) and connect to the device(s). Once connected the communication is done directly between the device and the desktop server. There are no firewall issues as the desktop creates an outgoing connection to the device.

Note that the online site used in this example should only be used for development. There are absolutely no guarantees regarding its availability.

I will explain how to set the board server when this project is ready. It is pretty simple and can even be done with a shared hosting server.

You can try the first draft version. Download the two attached zip files.
Choose a unique server name. It can be any string you want (letters and numbers only).
The server name is used by the board server to support multiple desktop servers.
In the desktop package, open config.properties and set the ServerName property as needed.
Run Run.bat to start the server.
In the device package (it is a B4A project) you will need to change this line to match the server name:
B4X:
    If FirstTime Then
        'Change test1 to the server name you choose!
        B4AServer.Initialize("http://66.84.14.57/android/b4a_server.php", "test1", "Main")
    End If
Also update Sub btnSendFile_Click and set the file to an existing file.

Check the Logs tab to see how it is working.

Your feedback and/or questions are more than welcomed!
 

ScarBelly

Member
Licensed User
Longtime User
I got it to work. Don't forget to add a check for file not found. Don't ask:sign0161:

Also, as a note to others, I discovered I needed to download the ByteConverter library.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think that the ByteConverter is not really needed. It is referenced by mistake.

The plan for B4AServer is to simplify in-house applications development.
Next week I will upload (at least) three examples for solving common tasks:
- Upload and print a file.
- Manage deployment of multiple in-house Android applications. Devices will connect to the server, download a file that lists the most recent applications, download the required APK files and install them.
- Automatically export data from a database and download it to the device. The data will be stored in an Excel file. Using the shell functionality the data will be exported to a CSV file which will be downloaded and parsed. Similar methods can be used to extract data from other databases.
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
Well, this sounds very good, and it will probably answer number of my questions. Can't wait to see it and test it. Wuldn't it be more beneficial to do it with some database instead of an Excell (or both). I haven't look at the B4A-Bridge source yet, but want to ask if this server examples would be based on Bridge App (?) Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The desktop server includes a "shell" functionality. It can execute any program you want and return the result. You will need to write a program that fetches the data from your database. The program is of course a Windows program (or Linux if running on Linux) and can be written with any programming language you want.
The planned Excel example is just an example.

The server is not based on the Bridge source. Though there are some similarities.
 
Upvote 0

ScarBelly

Member
Licensed User
Longtime User
This will definitely be useful for my in-house apps. I'll be pulling and pushing small lists from the client to the server.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Great work, Erel!
This will definately be worthful for me and other users.
I see that the server is written in java, will it also be available in vb(.net)?
As i'm more confident with VB then with Java.

:sign0098:

XverhelstX

Edit:

I cannot seem to receive the file.
This error on the batch:

C:\Users\Admin\Desktop\b4aserver>java -cp b4aserver.jar anywheresoftware.b4a.b4a
server.Server
B4A server is started. Checking URL: http://66.84.14.57/android/b4a_server.php,
server name=rootsoft
trying to connect to: 10.44.10.143:7890
Error occurred while working with ip: 10.44.10.143:7890
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at anywheresoftware.b4a.b4aserver.Communicator.run(Communicator.java:36)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source
)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

B4X:
'Change test1 to the server name you choose!
      B4AServer.Initialize("http://66.84.14.57/android/b4a_server.php", "rootsoft", "Main")

I also port forwarded port 7890 to see if that did something, but nope.

XverhelstX

EDIT:

Ok, that's why i still don't understand it.
When I use the program without wifi and just with my internet connection, I receive the error above.
When I enable my wifi, It works and it shows the background logo.
Any idea's on this?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The server will be expandable through its "shell" interface. You will be able to write programs with any language and then configure the server to run them as needed.

10.44.10.143 is your device IP, right?
Check the device logs and see if there are any relevant messages. The device should listen to this port waiting for connections. It is also possible that your router doesn't allow communication inside the network. You may need to explicitly open this port in the router firewall.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Port forward: Port 7890 done
Firewall: off

Logs when connected to pc with wifi:

** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Service (b4aserverservice) Start **
Server not connected. Calling board web service.
Http success. Waiting for server to connect...
Connected = true
Sending Task #6
6 done. Success=true, msg=File saved: /mnt/sdcard/6
Connected = false

On server:
C:\Users\Admin\Desktop\b4aserver>java -cp b4aserver.jar anywheresoftware.b4a.b4a
server.Server
B4A server is started. Checking URL: http://66.84.14.57/android/b4a_server.php,
server name=rootsoft
trying to connect to: 192.168.1.112:7890
Connected to: 192.168.1.112:7890
192.168.1.112:7890: stop message received.

Logs when trying to connect to pc without WiFi ( So normal mobile connection over EDGE)

** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Service (b4aserverservice) Start **
Server not connected. Calling board web service.
Http success. Waiting for server to connect...
** Activity (main) Pause, UserClosed = false **

On server: (when pressed a button.)

trying to connect to: 10.48.34.190:7890
Error occurred while working with ip: 10.48.34.190:7890
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at anywheresoftware.b4a.b4aserver.Communicator.run(Communicator.java:36)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source
)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


See the image attached or below.
XverhelstX

logs.png

http://i1189.photobucket.com/albums/z428/xverhelstx/logs.png
 

Attachments

  • logs.jpg
    logs.jpg
    26 KB · Views: 334
Last edited:
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
It is possible that your ISP is blocking incoming connections or blocking custom port numbers. This is a connection issue not related to B4AServer.
Probably, does this feature work on yours or someone else without using wifi?

B4AServer is intended for use in local networks (for in-house applications).

So, it is not possible to connect with EDGE (outside your house) to B4AServer then?
Because I want to add a feature to Tracked! that you can control the phone on your desktop too.

XverhelstX
 
Last edited:
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
I had the same problem over here:
http://www.b4x.com/forum/basic4android-updates-questions/8675-network-problems.html
Why doesn't this work then?

Attached is a program in vb 2008 including the source where you can connect to another ip (originally designed for a Trojan (;p) was just a test of it.)

Again here, I am able to connect to my device over wifi but not over 3g.

Should I contact my ISP, is it a "bug" in my router settings (linksys) or anything else?

XverhelstX

EDIT:

According to http://www.canyouseeme.org/

it cannot see my port open on 7890. So it's probably the ISP.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
(originally designed for a Trojan (;p) was just a test of it.)
Exactly for this reason it is not possible to connect to any IP you like. Incoming connections are blocked by default by your ISP firewalls. Only outgoing connections are allowed. This discussion is getting a bit off-topic. Please start a new thread if you have any more questions regarding global networking. As I wrote above, B4AServer should be used in a local network.
 
Upvote 0

beniendharto

Member
Licensed User
Longtime User
how to connect and get data from mysql using HttpUtils in service module

I'm still newbie in basic4android..
i want to make a project which connect and get data from mysql using HttpUtils in service module (which will running in boot) , and that service should be run every 5 minutes and i want the data that i got from database is displayed in LogCab.. :sign0163: help me how i could do that?..
 
Upvote 0
Top