Android Question Timer to send a series of asyncstream socket commands

Discussion in 'Android Questions' started by Mousa Najafi, Feb 14, 2015.

  1. Mousa Najafi

    Mousa Najafi Member 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Why are you disconnecting each time?

    Why not keep an open connection?
     
  3. Mousa Najafi

    Mousa Najafi Member 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.
    https://github.com/Steppschuh/RemoteControlServer/releases
    here is the listen method code:
    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Use B4J to create a real http server. It will be simpler and more reliable. You will then use HttpUtils2 to send the messages to the server.
     
  5. Mousa Najafi

    Mousa Najafi Member 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:
    Code:
    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); 
                            }
                  }
    }
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Go over B4J tutorials. There are many server examples and also examples of using the robot class.
     
  7. Mousa Najafi

    Mousa Najafi Member Licensed User

    Thanks Erel I've found samples.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice