B4J Question AsyncStream fails over time

mrossen

Active Member
Licensed User
Longtime User
Hi,

I have my doubts where to post this problem.

I have a ESP8266 module reviving data from a device and pass it through WiFi to B4j/B4a/B4I

I use the same code in B4j/B4A to get data.

B4X:
Sub AStreams_NewData (Buffer() As Byte)
    
    Dim msg, debugstr As String
    Dim i As Int
    
    msg = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
    
    dData = dData & msg
    
    For i = 0 To dData.Length - 1
        RecievedText(i) = Asc(dData.CharAt(i))
        debugstr = debugstr & Asc(dData.CharAt(i))
        'Log(RecievedText(i))
    Next


    Log(debugstr)
    Log(dData.Length)
    

    For i = 0 To 149
        RecievedText(i) = ""
    Next
    dData = ""
    msg = ""


    
End Sub

It is the same problem Standard/Prefix ASyncStream

The code receive between 50 to 2000 lines then it stops and throw an error.

B4J error:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
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)
java.net.SocketException: Software caused connection abort: recv failed

B4A error:

android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)~i

I have tried lot of adjustments, but nothing helps.

If I only have connection between device and ESP8266 there is no problem.

The problem seems to be from ESP8266 to java/android.

I dont know if anybody can help me from this information?

If you need more information I will post it.

Mogens
 

Daestrum

Expert
Licensed User
Longtime User
What is dData used for?

You add msg to it, but at the end of the sub you clear it, so could you just use msg instead?

You could probably rewrite the sub to speed it up similar to

B4X:
Sub AStreams_NewData (Buffer() As Byte)
 Dim msg As String = BytesToString(buffer,0,buffer.Length,"ISO-8859-1")
 Dim receivedText() As Byte = msg.GetBytes("ascii")
 Dim debugstr As StringBuilder
 debugstr.Initialize
 For Each b As Byte In receivedText
  debugstr.Append(b) ' much faster and doesn't keep creating strings
 Next
 Log(debugstr.toString)
 Log(msg.Length)
 msg=""

End Sub
 
Last edited:
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi Daestrum,

Thanks for your reply,

It is my novice way to read the data from the buffer.. I can see your way is much more elegant.

But it do not solve the problem with the broken connection

Mogens
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…