B4J Question exchanging data between b4j apps locally

Konrado

Member
Hi,
I wrote two apps in b4j that work on one pc and I want them to communicate (exchanging not more than twenty bytes per second). After many hours of searching for a solution at this forum I must ask You for a help. I didnt find any simple (and efficient) solution.
The only idea that comes to my head is to use reading and writing to same csv file but it seems to be a pretty clunky.
Any ideas?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
If the two apps are on the same PC or on the same network with access to the same network-accessible storage, then you can use the jFileWatcher library to facilitate inter-app communication. The library will raise an event whenever a file/folder inside a designated directory is modified, created or deleted. This, combined with writing serialized B4X objects to file with the RAF library, enables some very simple yet powerful inter-app communication possibilities.
 
Upvote 0

Konrado

Member
If the two apps are on the same PC or on the same network with access to the same network-accessible storage, then you can use the jFileWatcher library to facilitate inter-app communication. The library will raise an event whenever a file/folder inside a designated directory is modified, created or deleted. This, combined with writing serialized B4X objects to file with the RAF library, enables some very simple yet powerful inter-app communication possibilities.
Thanks. I know that library but in my project Iwant to get rid of continus reading and writing to file.
 
Upvote 0

Konrado

Member
There are several ways to communicate between two apps.

The simplest is to either use a TCP socket (ServerSocket + Socket + AsyncStreams) or a UDP socket (UDPSocket + UDPPacket).
Thanks Erel. My B4j apps work very well with UDP on different devices (with unique ip) but what should I do if I want them work on one pc? Any simple example on this forum?
 
Upvote 0

Konrado

Member
I've got some questions:
1. Do I have to intialize SAME PORT on both apps (I get error when I tried to do it on second app).
f.e.: UDPSocket.Initialize ("UDP", 5001, 8192)
2. Is there any range of ports available for this kind of usage
3. Do I have to set SAME IP on both apps. I wonder if there will be communications confilcts (like echo)
4. Is there any way to get UDP packets by two b4j apps (working on one pc with one ip). Packets are sent from other device with different ip (eps8266)
 
Upvote 0

Konrado

Member
Making an example. The task is to send at every one second array of 3 bytes from sender (app nr1) to receiver (app nr2) and both of these apps work on same device (one Pc with one ip). So have a quick look at the abbreviated listings. What have i forgotten?

Sender:

B4X:
Sub Process_Globals
    Dim UDPSocket As UDPSocket
    Dim Packet As UDPPacket
    Dim timer1 As Timer
    Dim list1() As Byte = Array As Byte (1,2,3)
End Sub

Sub AppStart (Form1 As Form, Args() As String)   
    timer1.Initialize("timer1", 1000)
    timer1.Enabled = True
    If  Not (UDPSocket.IsInitialized) Then 
        UDPSocket.Initialize("UDP", 5000, 8192)
    End If
End Sub

Sub timer1_Tick
        Packet.Initialize(list1, "127.0.0.1", 5000)
        UDPSocket.Send(Packet)
End Sub

Receiver
B4X:
Sub Process_Globals
    Dim UDPSocket As UDPSocket
    Dim Packet As UDPPacket
End Sub

Sub AppStart (Form1 As Form, Args() As String)   
   
End Sub

Sub UDP_PacketArrived (UDPsPacket As UDPPacket)
    Log (UDPsPacket.Length)
End If
 
Upvote 0

Konrado

Member
Thanks for help. Solved it.
In the receiver I've used wrongly:
...
UDPSocket.Initialize("UDP", 5000, 8192)
...
Packet.Initialize(list1, "127.0.0.1", 5000)

instead of correctly:
...
UDPSocket.Initialize("UDP", 5000, 8192)
...
Packet.Initialize(list1, "127.0.0.1", 5001)
 
Upvote 0
Top