Astreams - initializeprefix

derez

Expert
Licensed User
Longtime User
Hi
I have an application for chat and file transfer between two devices using wifi.
The chat (using short strings as messages) works in both cases - initialize and initializeprefix, but the file transfer works only with initialize.
The problem looks as if the data of the file itself (after the name and size are recieved) is not reaching the newdata sub.

here is the sending part:
B4X:
Sub send_string(st As String)
Dim buffer() As Byte
buffer = st.GetBytes("UTF8")
AStreams.Write(buffer)
End Sub

Sub send_file(dir As String)
Dim buffer(8192) As Byte

print("Sending " & fname, False)
raf.Initialize( dir, fname,True)
size = raf.Size  ' file size
total = size
Mult = 100 /total     

buffer = bc.StringToBytes( fname ,"UTF8")  ' name
AStreams.Write(buffer)

Dim buffer(8) As Byte

raftmp.Initialize3(buffer,True)
raftmp.WriteLong(size,0)        ' size
AStreams.Write(buffer)
raftmp.Close
bar.Progress = 0
sendtimer.enabled = True
End Sub

Sub sendtimer_tick
Dim buffer(8192) As Byte
Log("send size= " & size)
If size <= 0 Then
   raf.Close
   sendtimer.Enabled = False
Else
   count = raf.ReadBytes(buffer, 0, Min(8192,size), position)
   Astreams.Write(buffer)
   size = size - count   
   position = position + count
   bar.Progress = (total-size)* mult
End If
End Sub

and here is the recieving part:
B4X:
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
Dim result As Int

Select msgtype
   Case 0
      msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
      msg = msg.Replace(Chr(19),"")
      Select msg
         Case  "$ready$" 
            readyflag = True
            If readyflag AND fileselectedflag Then
               send_file(directory )
            End If
         Case "$send$"
            result = Msgbox2("האם אתה מאשר קבלת קובץ ? " ,"אישור העברת קובץ","כן", "ביטול","לא",Null)
            If result = DialogResponse.POSITIVE  Then 
               send_string("$ready$")
               msgtype = 1
            Else
               send_string("$notready$")
            End If
         Case "$notready$"
            readyflag = False
            fileselectedflag = False
            Print( "העברת הקובץ נדחתה" , False)
         Case Else
            Print(msg, True)
      End Select
   Case 1
      fname = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
      fname = fname.Replace(Chr(19),"")
      print("Recieving " & fname,False)
      raf.Initialize(File.DirRootExternal & "/recieved" , fname, False)
      msgtype = 2

   Case 2   
      raftmp.Initialize3(buffer,True)
      sizeR = raftmp.ReadLong(0)
      raftmp.close
      totalR = SizeR
      positionR = 0
      multR = 100 /totalR             
      bar.Progress = 0
      msgtype = 3

   Case 3
      rcvtimer.Enabled = False
      count = buffer.Length
      Log("size= " & sizeR & " count= " & count)
      raf.WriteBytes(buffer, 0, count, positionR)
      SizeR = SizeR - count
      positionR = positionR + count
      bar.Progress = (totalR-sizeR)* multR
      If sizeR <= 0 Then
         raf.Close
         msgtype = 0
         send_string(fname & " Recieved")
      Else
         rcvtimer.Enabled = True
      End If
End Select
End Sub

Sub rcvtimer_tick
If sizeR <= 0 Then
   raf.Close
   msgtype = 0
   send_string(fname & " Recieved")
   rcvtimer.Enabled = False
Else
   lbl(lcount-1).Text = "Waiting for data"
End If
End Sub
Sorry for the Hebrew part - it is consent or reject of the file by the reciever.

Edit - forgot to mention that the timers are init with this:
B4X:
sendtimer.Initialize("sendtimer",300)
sendtimer.Enabled = False
rcvtimer.initialize("rcvtimer",10000)
rcvtimer.Enabled = False
Can anyone tell me why it doesn't work with prefix - does not enter " case 3" ?
Thanks.
 
Last edited:

derez

Expert
Licensed User
Longtime User
Yes:
B4X:
Case  "$ready$" 
   readyflag = True
   If readyflag AND fileselectedflag Then
          send_file(directory )
   End If

readyflag is the result of recieving "$ready" and fileselectedflag is the result of file dialog to select the file to be sent, so both need to be true.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The sending device:

** Activity (main) Resume **
Displayed activity dudu.chatnet/.main: 985 ms (total 985 ms)
GC_EXPLICIT freed 3021 objects / 183072 bytes in 33ms
GC_EXPLICIT freed 156 objects / 7680 bytes in 30ms
GC_EXPLICIT freed 453 objects / 18976 bytes in 55ms
Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@442918d8
send size= 54257
sending count = 8192 size = 46065
send size= 46065
sending count = 8192 size = 37873
send size= 37873
GC_FOR_MALLOC freed 5153 objects / 339152 bytes in 39ms
sending count = 8192 size = 29681
send size= 29681
sending count = 8192 size = 21489
send size= 21489
sending count = 8192 size = 13297
send size= 13297
sending count = 8192 size = 5105
send size= 5105
sending count = 5105 size = 0
send size= 0
GC_EXPLICIT freed 346 objects / 12688 bytes in 30ms
GC_EXPLICIT freed 390 objects / 16544 bytes in 41ms
GC_EXPLICIT freed 1081 objects / 53960 bytes in 32ms

The recieving:

Start proc dudu.chatnet for activity dudu.chatnet/.main: pid=4493 uid=10084
** Activity (main) Resume **
Displayed activity dudu.chatnet/.main: 1009 ms (total 1009 ms)
GC_EXPLICIT freed 2817 objects / 175816 bytes in 33ms
GC_EXPLICIT freed 156 objects / 7688 bytes in 29ms
GC_EXPLICIT freed 262 objects / 12312 bytes in 40ms
GC_EXPLICIT freed 146 objects / 5672 bytes in 30ms
GC_EXPLICIT freed 198 objects / 9768 bytes in 40ms
GC_EXPLICIT freed 881 objects / 46864 bytes in 32ms
Jit: resizing JitTable from 16384 to 32768
Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
Aggregate from 1323030519787 (log), 1323030519787 (data)
GC_EXPLICIT freed 2629 objects / 235800 bytes in 35ms
Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44251ca0
name = 24.png
rec size = 1263224
ACTION_BATTERY_CHANGED pluggedType: 0
ACTION_BATTERY_CHANGED pluggedType: 0

As I said before - the name and size are recieved correctly.
I am using two devices - A81E android 2.2 and Asus transformer with 3.2, and the problem happens in both ways.
Maybe this is the hint ?

Maybe this:
"Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
Aggregate from 1323030519787 (log), 1323030519787 (data)"
I remember trying with smaller buffer, not with larger.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Files should be any size.
I succeeded to send files of 5MB with the astreams.initialize, and of course small ones of 20-30 KB
 
Upvote 0
Top