B4J Question Command line args

Discussion in 'B4J Questions' started by hakha4, Jun 19, 2015.

  1. hakha4

    hakha4 Member Licensed User

    Erel! I wrongly posted this on B4A forum,can you remove that thread,I don't know if I can do that.

    I made a small B4j app launced by a bat-file from a Pc prog for communicate to a Raspberry pi and it works ok. It consumes some time though to launch the B4j app everytime a msg is to be sent (every 5-10 secs). To speed up things I want the app to be launched just once.Is there a way to make a B4j app to listen for incomming commands (like vb6 'command$') ? I have not find info about this on forum.

    Any tips on how to optimize code below also welcome!



    Code:
    'Non-UI application (console application)
    #Region  Project Attributes
    '  #CommandLineArgs: TEST
    #End Region

    Sub Process_Globals
    Dim TcpStreams             As AsyncStreams
    Dim Socket1             As Socket
    Dim StringToSend As String
    End Sub

    Sub AppStart (Args() As String)
       
    If Args.Length = 0 Then
         
    Log("Arguments missing")
         ExitApplication2(
    1)
       
    End If
     
       
    'socket for connecting to 'Raspberry Pi-gateway on port 32340
        Socket1.Initialize("Socket1")
        Socket1.Connect(
    "192.168.1.4",32340,5000)
        StringToSend = Args(
    0)
       
           
       StartMessageLoop
    End Sub



    Sub Socket1_Connected(Connected As Boolean)As Boolean
        
    If Connected = True Then
       
            TcpStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,
    "tcpStreams")
           
            
    Try
                SendData(StringToSend)
                
    Catch
                
    Log("Send Error")
            
    End Try   
        
    End If
    End Sub

    Sub SendData(Msg As String)'to Pi Gateway (port 32340)
        Dim Buffer() As Byte
         
        
    If Socket1.Connected = True Then
            
    Try
                Buffer = Msg.GetBytes(
    "UTF8")
                TcpStreams.Write(Buffer)
                
    Log("Data sent")
                ExitApplication2(
    1)
            
    Catch
                
    Log("TcpStreams.Write Error")
            
    End Try
        
    Else
            
    Log("Connection lost")
        
    End If
           
    End Sub
    Regards Håkan
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You cannot listen to new command line arguments.

    You can open a socket and listen for connections. However I think that a much better solution will be to implement a small http server and then you can communicate with it through the browser. It is quite simple.
     
  3. hakha4

    hakha4 Member Licensed User

    Thank's for reply. I've read most of the tutorials but I'm a bit confused and unsure where to start. I need a small frame for a http server to start with,can you point me to that?
    Regards Håkan
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. hakha4

    hakha4 Member Licensed User

    Thank's,I'll start there.
     
Loading...