B4J Question Asyncstreams How to sent a Server acknowledgement to a Client


Well-Known Member
Licensed User
Longtime User

what is the best way to get a B4J server acknowledgement to a B4A/B4J client after the client has sent text or a file, using asyncstreams.

Looking for a way to ensure the client knows if the transaction has been successfull (or not).


Licensed User
Longtime User
You have to manage a simple protocole, by keeping a variable which stores the status. You probably send first the file name, then the size and then the whole file.
Each side keeps acount of the file size, as it is reduced to 0 - it is the end of the transmission.
The status variable is used in the recieving sub, for each status the response is different.
Here is a partial example of doing it:
Sub AStream_NewData (buffer() As Byte)
Dim st As String
If rcvr Then
    Select msgtype
        Case 0
            msgtype = 1
        Case 1
            msgtype = 2
        Case 2  
                msgtype = 3
        Case 3
            Rcount = buffer.Length
            rafrcv.WriteBytes(buffer, 0, Rcount, Rposition)
            Rfilesize = Rfilesize - Rcount
            Rposition = Rposition + Rcount
            acumulator = acumulator + Rcount
            bar.Progress = 100 * acumulator / totalsize
            If Rfilesize <= 0 Then
                msgtype = 1
                If acumulator >= totalsize Then bar.Visible = False
                Sbuffer = bc.StringToBytes( "not busy"  ,"UTF8")
            End If
    End Select
    st = BytesToString(buffer, 0, buffer.Length, "UTF8")
    If st = "not busy" Then busy = False
End If
End Sub

The msg "not busy" is signaling the sending side that a file has been recieved (and he can send the next file)
Upvote 0