B4J Question Problem with AsyncStream.Write

skyracer90

Member
Licensed User
Hello,
via VB6 I send a string to my machine. The machine also answers. That means the string is ok.

If I now send the same string via B4J to the machine I get a Error.Send B4J the string differently?
Connection is init. and OK!

The error message shows me that the sent string is not OK. But it works with VB6!
Any Idea?

LOG:
Connected with 192.168.178.20
btn_Send
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.readNumberOfBytes(AsyncStreams.java:287)
at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:212)
at java.lang.Thread.run(Thread.java:748)
 

KMatle

Expert
Licensed User
Which "machine" do you mean? Can you see any error messages on it? Do you use prefix/non prefix? What about the string? (you send bytes, not a string). Maybe VB sends ASCII or an other codepage and B4J UTF8. Just try to change that parms.

PS: Any code would be helpful (VB and B4J)
 

skyracer90

Member
Licensed User
Hello KMatle,
Thank you for your Answer....
First Time i use this to build my "SendString":

Dim Query as String
Query = Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(194) & Chr(6) & Chr(1) & Chr(3)
Dim MyMessage As String = Query
Dim bytes() As Byte = MyMessage.GetBytes("UTF-8")
astream.Write(bytes)

It doesn't work!
Now i use the ByteBuilder Class! Look like this and it's work perfect!

Dim Query As BytesBuilder
Query.Initialize
'Query.Clear
Query.Insert(0, Array As Byte(0x00, 0x00, 0x00, 0x00, 0xC2, 0x06, 0x01, 0x03))
astream.Write(Query.ToArray)

https://www.b4x.com/android/forum/threads/b4x-bytesbuilder-simplifies-working-with-arrays-of-bytes.89008/

Thanks!
 

bdunkleysmith

Active Member
Licensed User
Good to see you have a solution @skyracer90, but if you are sending strings only you might like to take a look at the AsyncStreamsText class because as Erel says at the beginning of that reference "When a message (set of bytes) is sent over a network channel, of any type, there is no guarantee that the whole message will arrive as a single set of bytes."

I've used AsyncStreamsText myself with success in similar circumstances and so can recommend it.
 

Daestrum

Well-Known Member
Licensed User
B4X:
...
Dim Query as String
Query = Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(194) & Chr(6) & Chr(1) & Chr(3)
Dim bytes() As Byte = Query.GetBytes("windows-1252") ' 8 bit code page
astream.Write(bytes)
...
 

emexes

Well-Known Member
Licensed User
B4X:
...
Dim Query as String
Query = Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(194) & Chr(6) & Chr(1) & Chr(3)
Dim bytes() As Byte = Query.GetBytes("windows-1252") ' 8 bit code page
astream.Write(bytes)
...
Close, but for this:
B4X:
Dim bc As ByteConverter
  
Dim TS As String = Chr(100) & Chr(110) & Chr(120) & Chr(130) & Chr(140) & Chr(150) & Chr(160) & Chr(170)
Dim TA() As Byte = bc.StringToBytes(TS, "windows-1252")
For I = 0 To TA.Length - 1
    Log(Bit.And(TA(I), 0xFF))
Next
B4X:
Program started.
100
110
120
63
63
63
160
170
Hours of fun to be had, tracking that down :)
 
Top