1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Tutorial AsyncStreamsObject - Send and receive objects instead of bytes

Discussion in 'Tutorials & Examples' started by Erel, Jun 26, 2013.

  1. Erel

    Erel Administrator 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.

    [​IMG]

    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:
    Code:
    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(
    00, Panel1.Width, Panel1.Height)
             cvs.DrawBitmap(bmp, 
    Null, r)
             Panel1.Invalidate
          
    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.
    Code:
    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:
    Code:
    astreamO.Initialize(Me, "astreamO")
    Once there is a connection you should call AsyncStreamsObject.Start:
    Code:
    Sub StartAstream(s As Socket)
       astreamO.Start(s.InputStream, s.OutputStream)
       SetUIState
    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.
     

    Attached Files:

    Last edited: Nov 14, 2013
  2. tkristensen

    tkristensen Member 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?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. GMan

    GMan Well-Known Member Licensed User

    Hoi Erel,

    i always get an error:

    java.net.SocketException: Bad File number

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

    Erel Administrator Staff Member Licensed User

    The connection is not really related to AsyncStreamsObject class. Please start a new thread for this error and also post the full error message.
     
  6. GMan

    GMan Well-Known Member Licensed User

    ???
    I am using the sample code below - isn't that using the AsyncStreamsObject class ?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    I prefer that this thread will be used for discussions about AsyncStreamsObject functionality. AsyncStreamsObject is one level above the actual connection. You should just need to start a new thread.
     
  8. GMan

    GMan Well-Known Member Licensed User

    :sign0013: - problem is also solved
     
  9. merlin2049er

    merlin2049er Well-Known Member Licensed User

    Thanks, I'll try using this.
     
  10. Dominex

    Dominex Active Member Licensed User

    Thanks Erel,

    I have a question, a link between two devices in Wi-Fi works well, but if one of the two devices is under network data does not work, is this normal?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    This is not really related to AsyncStreamsObject.

    You cannot directly connect two devices over the internet. Direct connection will only work on a local network.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    AsyncStreamsObject was updated. Fixes two issues related to the temporary files created during the communication.
     
  13. Dominex

    Dominex Active Member Licensed User

    Thanks.
     
  14. Nenad Trickovic

    Nenad Trickovic Member Licensed User

    Is it possible to send multiple files simultaneously or to receive and send at the same time?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You can send and receive files at the same time. However you can only send one file at a time.
     
  16. Nenad Trickovic

    Nenad Trickovic Member 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?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    I'm not sure that I understand. AsyncStreams allows you to send multiple files. However they will be sent one after another. Eventually there is a single channel between the two sockets.
     
  18. Rusty

    Rusty 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?
    Thanks,
    Rusty
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Your question is not related to AsyncStreams or AsyncStreamsObject. You should start a new thread for it.

    AsyncStreams is not aware of the connection. The port number (and the connection type) doesn't matter.
     
  20. Douglas Farias

    Douglas Farias Expert Licensed User

    @Erel i made this
    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: Nov 19, 2014
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice