Socket communication Issue

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Guys

Quick Background:
I have a program that uses socket communication to exchange information with an embedded device.

The program operates perfectly ok on my old tablet.
I am running the same program on a Nexus 7 but it is not working properly.
In particular I don't think it is receiving all the messages from the embedded device.

To make it more clear, the embedded device sends out a message to the tablet every 2 seconds. When the tablet receive the message, we toggle an icon on the screen. On the old tablet, the icon toggles every 2 seconds as expected. On the nexus, you see the icon toggling at different intervals, then it stops. Unfortunately "Log" is not working on the nexus too, which is making it more difficult to debug.

Any help is greatly appreciated.


Thanks
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel
Finally I was able to get the USB to work . I had to uninstall the old Android SDK Manager, install the newest version. I also upgraded b4A to 2.02 ( to be on the safe side )

So the "Log" is working now.

Currently I am investigating the Socket Issue.

I will post my finding by end of the day.

Thanks
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

Here is what I have found out so far.

The embedded system, sends 2 packets of data every 2 seconds or so.( the first one is 71 bytes, and the second one 174 ) . With the old tablet I get 2 separate events in AStreams_NewData ( one for each )
With the nexus 7, I am getting a single event for both packets.
And the buffer read has both packets ( total length 71+174).

Any ideas?

Thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Different devices use different buffers. Messages can always be split or sent together. This is exactly the purpose of prefix mode. In your code you will not be able to use prefix mode as you do not control the embedded device protocol. You will need to check the message length and decide whether to split it or not.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
I will experiment with the Prefix mode. ( The embedded device was designed by me as well so that shouldn't be a problem)

Thanks for the reply
 
Upvote 0

gemasoft

Member
Licensed User
Longtime User
4 bytes with lenght

Erel, i use initialize only, but now i receive my json string truncate. How i can receive well this message using initialize without prefix. Because i need send and receive data from iPhone and Windows phone.

Another solution is answer, how i can simulate prefix protocol using Windows phone or iPhone?

how i can encode the lenght before the byte array? what format use? i can send 00 00 00 ff and after a 255 bytes packet? and when receive using prefix is ok? or what protocol i need use for encode the 4 byte int value?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Prefix mode takes care of correctly assembling the original messages from the raw data. In non-prefix mode you will need to take care of it yourself.

The protocol is pretty simple. You just need to send the message length and then the message.
The same is true when you read data.

c# code:
B4X:
BinaryWriter bw = new BinaryWriter(...);
byte[] msg = ...;
bw.Write(msg.Length); //write an int (32 bytes)
bw.Write(msg);
 
Upvote 0
Top