B4J Question Sending the package to the server (TCP)

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:
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
On the second button I send data to the server like this:
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
Finally I disconnect from the server like this:
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
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
 

vmag

Active Member
Thank you all for your attention! The problem was solved by artificial delay after each stage:
B4X:
serverOK=0
    server_connect
    Do Until serverOK = 1
        Sleep(100)
    Loop
 
Upvote 0
Top