B4J Question Send AsyncStream

Discussion in 'B4J Questions' started by Ivan Vidak, May 31, 2019.

  1. Ivan Vidak

    Ivan Vidak Member


    I am new at socket programming so i need help.
    I am trying to connect b4j client app to some server application (not created by b4x).
    Connecting to application works with no problem, but when data is sent it does't get recived by server app with command 'Astream.Write(txtInput.Text)'.
    There is two buttons on client app - 'btnSend_Click' and 'btnConnect_Click'.

    Here is how it works:
    First I click at 'btnConnect_Click' and connetion is established.
    Then click on send button 'btnSend_Click', but server app doesn't recive any data.
    But if I click on 'btnConnect_Click' then command is recived by server app.
    - it's like data is stored in buffer and waits for 'Astream.Initialize' to be sent.
    Tested it with AsyncSteam and AsyncSreamText and result is the same.

    If prefix mode is used then on 'Astream.Write(txtInput.Text)' data is received, but then data is prefixed with the bytes array, and I am not using b4x as Server.

    Does anyone have suggestion how to handle this?

    Thank You,

    Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private lblState As Label
    Private IsConnected As Boolean
    Private Astream As AsyncStreamsText
    Private socket As Socket
    Private txtInput As TextField
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
    "Client_layout"'Load the layout file.
    End Sub

    'Return true to allow the default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
    Return True
    End Sub

    Private Sub SetState (Connected As Boolean)
        IsConnected = Connected
    If IsConnected Then
            lblState.Text = 
    "State: Connected"
            lblState.Text = 
    "State: Disconnected"
    End If
    End Sub

    Sub btnConnect_Click
    If Astream.IsInitialized Then
    End If
    If socket.IsInitialized Then
    End If
    Wait For Socket_Connected (Succsessful As Boolean)
    If Succsessful Then
    '        Astream.InitializePrefix(socket.InputStream, True, socket.OutputStream, "astream")
    '        Astream.Initialize(socket.InputStream, socket.OutputStream, "astream")
            Astream.Initialize(Me, "Astream"socket.InputStream, socket.OutputStream)
    End If
    End Sub

    Private Sub Astream_NewData (Buffer() As Byte)
    Log(BytesToString(Buffer, 0, Buffer.Length, "utf8"))   
    End Sub

    Private Sub Astream_Terminated
    End Sub

    Private Sub Astream_Error
    End Sub

    Sub btnSend_Click
    Dim a As String = txtInput.Text
    '    Astream.Write(a.Name.GetBytes("UTF8"))
    End Sub
  2. Erel

    Erel Administrator Staff Member Licensed User

    Moved to the questions forum.

    There is no such thing.

    Your code is wrong. If you want to send text it should be:
    It is also possible that your server expects each message to end with an end of line character.
    Johan Hormaza likes this.
  3. Ivan Vidak

    Ivan Vidak Member

    Thanks Erel.
    Is there a way a communicating in syncronous way?
    And one more question. What is the best way of making control app (b4j, b4a, b4i) for other non b4x serever app? I don’t need to send data from server back to control app, just send data from control app to server app (tcp).

    Thank you,
  4. Erel

    Erel Administrator Staff Member Licensed User

    No. Network communication cannot be done on the main thread.

    Question is too vague to provide a useful answer.

    Check this thread: [B4X] The Networker's Guide To The Galaxy
    Last edited: Jun 19, 2019
  5. Ivan Vidak

    Ivan Vidak Member

    Thank you Erel.

    I manage to make it work.
    Server did expect each message to end with an end of line character, and it was 0.
    But then I still had problem with encoding it in UTF8, server did't read it correctly.

    So following code worked:

    Private delimiter As ByteConverter

    Dim a As String = "test_app|TestString"
    Astream.Write(delimiter.HexToBytes(" 0"))

    Thanks for pointing me in right direction.
  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