Android Question Timer to send a series of asyncstream socket commands

Mousa Najafi

Licensed User
I need to send a series of command to a PC server by asyncstream in regular mode. In every command, I write to stream
then close stream and socket then reconnect the client to server. in Client_connected event I enabled timer if the connection is successful.
this method gives different results with different timer intervals. sometimes works as intended but most of the time
do not send commands correctly. I think it is because when I write to asyncstream it's unclear when the write will be completed
How can I know when the write will be completed ?
Do you have better solution to above problem

Mousa Najafi

Licensed User
Because in server side in listen method when run reaches to tcpListener.AcceptTcpClient ; program UI blocks and closing socket reactivate the UI
.If you are interested here is the link of server which is open source project. It is the server of remote control collection app in google play.
here is the listen method code:
Public Sub listen()
            If tcpListener.Pending = True Then
                tcpClient = tcpListener.AcceptTcpClient
                tcpClient.ReceiveBufferSize = buffer

                Dim localEndPoint As Net.IPEndPoint = tcpClient.Client.LocalEndPoint
                Dim remoteEndPoint As Net.IPEndPoint = tcpClient.Client.RemoteEndPoint

                Dim Reader As New BinaryReader(tcpClient.GetStream)
                    Dim message_data As Byte()
                    message_data = Reader.ReadBytes(tcpClient.ReceiveBufferSize)

                    Dim command As New Command
                    command.source = remoteEndPoint.Address.ToString
                    command.destination = localEndPoint.Address.ToString
           = message_data

                    'Logger.add("LocalEndPoint: " & command.destination)
                    'Logger.add("RemoteEndPoint: " & command.source)

                Catch ex As Exception
                    Logger.add("Error while parsing TCP data:")
                End Try
            End If
        Catch ex As Exception
            Logger.add("Error while receiving TCP data:")
        End Try
    End Sub
I wrote to socket.outputstream in socket_connected event instead of initializing an asyncstream and writing to that.
In every command after writing command to outputstream I flushed then closed the stream . then reconnect the client socket
and repeat this process in socket_connected event. Up to now it worked well but I have to test it more

Mousa Najafi

Licensed User
That server was doing my purpose easilly . Just I need to connect that. remaining things were implemented in that program. but I have to pay the author
for commercial use.
please guide me how can I implement the following tasks in B4J:
1- design an easy connectable server
2- run platform independent desktop keyboard events in B4J (I know there is a java robot class that has this ability like the following code:

code to simulate input keyboard in java:
import java.awt.Robot
public static void type(String str){   
       Robot robot = new Robot();
       for(char ch:str.toCharArray()){      
                             char upCh = Character.toUpperCase(ch);