Ivan Vidak
Member
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
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