vmag
Active Member
This video tutorial has everything I need and it all works with separate buttons, but I can't achieve a comprehensive implementation.
For example the first button connects me to the server like this:
On the second button I send data to the server like this:
Результат приходит правильный так:
Finally I disconnect from the server like this:
Practically, I do everything according to the example above and I have everything working with three buttons, but I can't do it in one go in one button, that is, immediately in one procedure: connect to the server + send data + get a response + disconnect from the server. If I collect all this in a heap and share actions via fx.Msgbox, then everything works, if I remove fx.Msgbox, then data is not sent
For example the first button connects me to the server like this:
B4X:
Sub server_connect
SetState(False)
If astream.IsInitialized Then
astream.Close
End If
If socket.IsInitialized Then
socket.Close
End If
socket.Initialize("socket")
socket.Connect(txtHost.Text,txtPort.Text,0)
Wait For Socket_Connected (Succesful As Boolean)
If Succesful Then
SetState(True)
astream.InitializePrefix(socket.InputStream,True,socket.OutputStream,"astream")
End If
End Sub
B4X:
astream.Write("{""command"":""GetInfo""}".GetBytes("UTF8"))
B4X:
Private Sub Astream_NewData(Buffer() As Byte)
txtAnsver.Text = BytesToString(Buffer, 0 ,Buffer.Length, "utf8")
End Sub
B4X:
Sub server_disconnect
txtAnsver.Text=""
SetState(False)
If astream.IsInitialized Then
astream.Close
End If
If socket.IsInitialized Then
socket.Close
End If
End Sub