Android Tutorial AsyncStreamsObject - Send and receive objects instead of bytes


Staff member
Licensed User
AsyncStreamsObject is a small framework built over AsyncStreams.

Instead of sending and receiving bytes, AsyncStreamsObject allows you to send and receive objects. This makes it much simpler to communicate with other devices.

AsyncStreamsObject uses the WriteStream method of AsyncStreams. This method was introduced in RandomAccessFile v1.5.

You can use AsyncStreamsObject when you implement both sides of the connection.

Supported objects

WriteObject supports: Lists, Arrays, Maps, Strings, primitive types and user defined types.

WriteBitmap allows you to send bitmaps.

WriteFile allows you to send any file.

All the three write methods expect a key (string) and a value (the actual object).

The NewObject event is raised when an object is received. You should use the key to decide how to handle the object. This code is taken from the attached example:
Sub astreamO_NewObject(Key As String, Value As Object)
   Select Key
      Case "form"
         Dim m As Map = Value 'Value is a Map
         txtFirst.Text = m.Get("first")
         txtLast.Text = m.Get("last")
         txtAnimal.Text = m.Get("animal")
      Case "simple value"
         Dim number As Int = Value
         ToastMessageShow("Received lucky number: " & number, False)
      Case "image"
         Dim bmp As Bitmap = Value
         Dim r As Rect
         r.Initialize(0, 0, Panel1.Width, Panel1.Height)
         cvs.DrawBitmap(bmp, Null, r)
      Case "file"
         Dim fileName As String = value
         Log("Received file. File size: " & File.Size(astreamO.TempFolder, fileName))
   End Select
End Sub
The ObjectSent event is raised after a value was successfully sent.
Sub astreamO_ObjectSent (Key As String)
   Log("Object sent: " & Key)
End Sub
Initializing AsyncStreamsObject

Two steps are required. First you should call AsyncStreamsObject.Initialize and pass the target module and event name:
astreamO.Initialize(Me, "astreamO")
Once there is a connection you should call AsyncStreamsObject.Start:
Sub StartAstream(s As Socket)
   astreamO.Start(s.InputStream, s.OutputStream)
End Sub
Note that you can use any type of connection that provides an input stream and output stream.

The Terminated event is raised when the connection is closed.

The attached example connects two devices over the local network. You need to set the IP address of one of the devices and press on the Connect button. Once connect you can send data between the devices.

AsyncStreamsObject class is included in the attached example.


Last edited:


Licensed User
I'd like to use the AsyncStreamsObject to receive and send files to our PC based host. Has anyone implemented the PC side of AsyncStreamsObject for use with .net?


Well-Known Member
Licensed User
Hoi Erel,

i always get an error: Bad File number

Doesnt matter, if i a target ip is given or not.


Well-Known Member
Licensed User
The connection is not really related to AsyncStreamsObject class.
I am using the sample code below - isn't that using the AsyncStreamsObject class ?


Well-Known Member
Licensed User
:sign0013: - problem is also solved

Nenad Trickovic

Licensed User
How could we achieve functionality to send more files at the same time and to receive more files at the same time?

Can we create the class that wraps the code as in example and later instantiate it per each connection?


Well-Known Member
Licensed User
I'm trying to use the AsyncStreams with prefix and have a requirement to use port 443. I seem to remember there is a limit on the port numbers below (about) 2000. Can you advise me on how I can use port 443?

Douglas Farias

Licensed User
@Erel i made this
Sub btnSendFile_Click
CC.Show("*/*", "Choose file to send")
End Sub

Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
File.Copy(Dir,FileName, fp, "temporario.dat" )
astreamO.WriteFile("file", fp, "temporario.dat")
End If
End Sub
i think the code its correct, but dont send to the pc, on the pc only a file is generated ip.txt with my ip but not the file sent
and no logs with file sended sucefull

edit: solved i m noob , dont see temp folder ¬¬
Last edited: