B4J Tutorial AsyncStreamsObject - Send and receive objects over the network

For new projects it is recommended to use B4XSerializator instead of this solution.



SS-2013-11-14_13.26.52.png


AsyncStreamsObject allows you to send and receive objects instead of bytes.
You can read more about it here: http://www.b4x.com/android/forum/th...d-and-receive-objects-instead-of-bytes.30543/

This example connects to the same Android example and allows you to communicate with the device.
The device app acts as the server as the desktop firewalls usually blocks incoming connections.

You can download the Android example here: http://www.b4x.com/android/forum/th...e-objects-instead-of-bytes.30543/#post-202288
Make sure to update B4A RandomAccessFile library to v1.6: http://www.b4x.com/android/forum/th...handle-streams-of-any-size.30494/#post-177051

Once you have a valid connection it is very simple to send and receive custom types, drawings, collections and files.
 

Attachments

  • AsyncStreamsObject.zip
    4.1 KB · Views: 3,305
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
Ok, but AStreamO_NewObject on the b4a dont save the file, only give logs.

1° i m sending the key file:myfilename from b4j its correct now

2° on the b4a i m get this in AStreamO_NewObject with substring , this is ok

B4X:
Sub astreamO_NewObject(Key As String, Value As Object)
    If Key.StartsWith("arquivo:") Then
    Log(Key.SubString(8))
            Dim fileName As String = Value
            Log(fileName)
            Log("Received file. File size: " & File.Size(astreamO.TempFolder, fileName))
  End If
   
End Sub

but the problem is how to write a file with this name now, the astreamO_NewObject dont save the file, i think this code is in your class on b4a.
i m sending the correct name, i m geting this correct name on b4a, and now how to save the file with correct name?
 

Attachments

  • Sem título.png
    Sem título.png
    209.7 KB · Views: 470

Douglas Farias

Expert
Licensed User
Longtime User
done, i have made this
B4X:
    If Key.StartsWith("arquivo:") Then
    Log(Key.SubString(8))
            Dim fileName As String = Value
            File.Copy(astreamO.TempFolder,fileName, fp , Key.SubString(8))
            Log(File.ListFiles(fp))
    End If
;) ty @Erel
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
when i use this code, with big files, i have a small bug

B4X:
If Key.StartsWith("arquivo:") Then
            Dim fileName As String = Value
            log("aqui")
            File.Copy(astreamO.TempFolder,fileName, fp , Key.SubString(8))
            Log("done")
    End If

for exemple, send from pc a video with 60mb, on the b4a it make a laggg when make this File.Copy
i see the log "aqui" like the code. log("aqui")

my app stop respose for 5 or 10 seconds, i have try make this


B4X:
If Key.StartsWith("arquivo:") Then
            Dim fileName As String = Value
            log("aqui")
            ProgressDialogShow2("carregando...", False)
            DoEvents
            File.Copy(astreamO.TempFolder,fileName, fp , Key.SubString(8))
            Log("done")
    End If

but dont work with doevents, my app stop response too with
B4X:
ProgressDialogShow2("carregando...", False)
         DoEvents

and i dont see the ProgressDialogShow2 only the log "aqui"

and later my app stop work for 5 - 10 seconds i see "Done" the last log.

now the question, why this? i m know the file is bigger 60mb, but have a way to my app dont stop work when make a file.copy?
and how can i add a progress on this? ProgressDialogShow2("Copying...", False), later done ProgressDialogHide
 

Douglas Farias

Expert
Licensed User
Longtime User
why use a library to make a sub? can u give a simple example code?

callsubcopy(folders, file)

sub copy
file.copy
end sub
 

Douglas Farias

Expert
Licensed User
Longtime User
this dont helped my @Erel
look i made this

B4X:
        'VIDEOS
        Else If Key.SubString(8).StartsWith("videos:") Then
        Dim fileName As String = Value
        File.MakeDir(fp, "Videos")
        pasta = File.Combine(fp, "Videos")
        ProgressDialogShow2("carregando...", False)
        Private l As List
        l.Initialize
        l.AddAll(Array As String(astreamO.TempFolder, fileName, pasta, Key.SubString(15)))
        TR.RunOnGuiThread("copiar_arquivos" , Array As Object(l))


Sub copiar_arquivos(MapObj As Object)
Private l As List
l.Initialize
l = MapObj
File.Copy(l.Get(0), l.Get(1), l.Get(2), l.Get(3))
ProgressDialogHide
End Sub

but my app stop work too with RunOnGuiThread.
only with big sizes the app stop works, 60mb+

EDIT: 03:06
B4X:
        'VIDEOS
        Else If Key.SubString(8).StartsWith("videos:") Then
        Dim fileName As String = Value
        File.MakeDir(fp, "Videos")
        pasta = File.Combine(fp, "Videos")
        Private l As List
        l.Initialize
        ProgressDialogHide
        ProgressDialogShow2("Copiando...", False)
        l.AddAll(Array As String(astreamO.TempFolder, fileName, pasta, Key.SubString(15)))
        TR.Start(Me, "copiar_arquivos" , Array As Object(l))

this code works, with tr.start

thx erel
 
Last edited:

YIM bunchhat

Active Member
Licensed User
Longtime User
I cannot send file and receive file. How to send file from phone to pc? Ex: file .pdf in sd card.....:(. Could you write me simple code that work only send file from phone to PC? your example is complicated for me.......now I am really headache...:(
 
Last edited:
Top