Android Question Timer to send a series of asyncstream socket commands

Mousa Najafi

Member
Licensed User
Longtime 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

Member
Licensed User
Longtime 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.
https://github.com/Steppschuh/RemoteControlServer/releases
here is the listen method code:
B4X:
Public Sub listen()
        Try
            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)
                Try
                    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
                    command.data = message_data
                    command.process()

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

                    Reader.Close()
                Catch ex As Exception
                    Logger.add("Error while parsing TCP data:")
                    Logger.add(ex.ToString)
                End Try
            End If
        Catch ex As Exception
            Logger.add("Error while receiving TCP data:")
            Logger.add(ex.ToString)
        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
 
Upvote 0

Mousa Najafi

Member
Licensed User
Longtime 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:
B4X:
import java.awt.Robot
public static void type(String str){   
       Robot robot = new Robot();
       for(char ch:str.toCharArray()){      
                 if(Character.isUpperCase(ch)){      
                              robot.keyPress(KeyEvent.VK_SHIFT);         
                             robot.keyPress((int)ch);     
                             robot.keyRelease((int)ch);  
                             robot.keyRelease(KeyEvent.VK_SHIFT);      
                }else{
                             char upCh = Character.toUpperCase(ch);         
                             robot.keyPress((int)upCh);     
                             robot.keyRelease((int)upCh); 
                        }
              }
}
 
Upvote 0
Top