B4J Question How to make two tcp servers that exchange data?

amorosik

Expert
Licensed User
How to make two tcp servers that exchange data?
I would like to create a procedure in B4J that allows two external clients to connect as tcp clients, and to exchange information
In the sense that I would like every character sent by client1 to be read by client2
I find many examples to activate a single tcp server capable of accepting connections from the outside
But two different tcp server, ones responding on port 1111, the other on port 2222, I don't quite understand how to achieve them
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

amorosik

Expert
Licensed User
You don't need two ports to support two clients.
Example of TCP server that supports multiple clients: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73792/#content
Another one: [B4X] FTP Server implemented with Socket and AsyncStreams

My recommendation: don't do it. Use MQTT instead. It will be simple to implement and will work better.
Or create a real server with jServer.

Thanks for the advice
I know, have already used, and greatly appreciate the functionality contained in the mqtt protocol
But in this case I have no control over external clients
And therefore I have to adapt the server to allow communication to devices that already use this system
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Thanks for the advice
I know, have already used, and greatly appreciate the functionality contained in the mqtt protocol
But in this case I have no control over external clients
And therefore I have to adapt the server to allow communication to devices that already use this system
"question": but what "system" are really working ?
If it is a custom-tcp-way (or udp) you must have a guide or at least some "code" at other language...
 
Upvote 0

amorosik

Expert
Licensed User
"question": but what "system" are really working ?
If it is a custom-tcp-way (or udp) you must have a guide or at least some "code" at other language...

I don't quite understand what you mean
But besides the server made in B4J there is nothing else as my contribution to the project
If you mean external clients, they are actually hardware devices
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
I don't quite understand what you mean
But besides the server made in B4J there is nothing else as my contribution to the project
If you mean external clients, they are actually hardware devices
yes i mean the h/w devices... what actually sending... are you sure not using any specific protocol... ?
 
Upvote 0

amorosik

Expert
Licensed User
yes i mean the h/w devices... what actually sending... are you sure not using any specific protocol... ?

Client1 is a gps fixed base that sends differential corrections
Client2..N are classic gps receivers (actually the communication is between client1 and N different clients that only 'read')
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
I see... so it is like a GPS - NMEA stream string... so not any special protcol... Socket and AsyncStreams as Erel said will do the job (first part)... then you must filter the incoming.. somewhere saw how to read a serial gps (nmea interpreter)... here it is - may that help you (for the second part)
 
Upvote 0

amorosik

Expert
Licensed User
I see... so it is like a GPS - NMEA stream string... so not any special protcol... Socket and AsyncStreams as Erel said will do the job (first part)... then you must filter the incoming.. somewhere saw how to read a serial gps (nmea interpreter)... here it is - may that help you (for the second part)


aaa_gps_routing.jpg


It's not Nmea sentences, but a sort of binary char sequence that flow from base to gps receiver
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is a Java Version of a NTRip-client.
The interesting part is
B4X:
private String CalculateChecksum(String line) {
        int chk = 0;
        for (int i = 0; i < line.length(); i++) {
            chk ^= line.charAt(i);
        }
        String chk_s = Integer.toHexString(chk).toUpperCase(); // convert the integer to a HexString in upper case
        while (chk_s.length() < 2) { // checksum must be 2 characters. if it falls short, add a zero before the checksum
            chk_s = "0" + chk_s;
        }
        return chk_s;
    }
to generate the Checksum for the NMEA-Command you send
 
Upvote 0
Top