B4J Question Send AsyncStream

Hi.

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,
Ivan


B4X:
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
    MainForm.RootPane.LoadLayout("Client_layout") 'Load the layout file.
    MainForm.Show
    SetState(False)
End Sub

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

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

Sub btnConnect_Click
    SetState(False)
    If Astream.IsInitialized Then
        Astream.Close
    End If
    If socket.IsInitialized Then
        socket.Close
    End If
    socket.Initialize("socket")
    socket.Connect("localhost", 6100, 0)
    Wait For Socket_Connected (Succsessful As Boolean)
    If Succsessful Then
        SetState(True)
'        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
    SetState(False)
End Sub

Private Sub Astream_Error
    Astream_Terminated
End Sub

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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Moved to the questions forum.

it's like data is stored in buffer and waits for 'Astream.Initialize' to be sent.
There is no such thing.

Your code is wrong. If you want to send text it should be:
B4X:
Astream.Write(txtInput.Text.GetBytes("UTF8"))

It is also possible that your server expects each message to end with an end of line character.
 
Upvote 0
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,
Ivan
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is there a way a communicating in syncronous way?
No. Network communication cannot be done on the main thread.

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).
Question is too vague to provide a useful answer.

Check this thread: [B4X] The Networker's Guide To The Galaxy
 
Last edited:
Upvote 0
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(a.GetBytes("UTF8"))
Astream.Write(delimiter.HexToBytes(" 0"))



Thanks for pointing me in right direction.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…