Android Question Send simple command to PC using AsyncStreams

dim

Member
Licensed User
Longtime User
Hi all,
I am using the code that follows to send the command "TEST" to an application running on a PC. When I send it, the PC receives something but it's an empty string.

Also, although my PC application is configured to reply with an error code when an empty message is received, altough it replies, nothing is received in _NewData event.

Can you please figure out what I am doing wrong ?
Thank you.


B4X:
Sub Globals
    Private Panel1 As Panel
    Private Button1 As Button
    Private EditText1 As EditText
    Private button_exit As Button
   
    Dim as1 As AsyncStreams
    Private ListView1 As ListView
    Dim counter1 As Int
   
    Dim socket1 As Socket
    Dim stream1 As AsyncStreams
    Dim server_reply As String
    Dim command_1 As String
End Sub


Sub append_status (new_status As String)
    counter1 = counter1 + 1
    ListView1.AddSingleLine(counter1 & ". " & new_status)
    ListView1.SetSelection(ListView1.Size-1)
End Sub


Private Sub socket1_connected (Successful As Boolean)
  If Successful Then
     append_status("Connection OK")
    command_1 = "TEST"
     stream1.Initialize(socket1.InputStream, socket1.OutputStream, "stream1")    
    stream1.write(command_1.GetBytes("ISO-8859-1"))
  Else
     append_status("Cannot connect")
  End If
End Sub


Private Sub stream1_error
   append_status("Stream1 error")
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout_main")
    counter1 = 0
End Sub


Sub button_exit_Click
    If socket1.Connected Then
       socket1.Close
    End If
    Activity.Finish
End Sub


Sub Button1_Click    'try connection
    append_status("Trying to connect...")
    socket1.Initialize("socket1")
    socket1.Connect("192.168.5.150", 8090, 5000)
End Sub


Sub stream1_NewData (Buffer() As Byte)
    append_status("Data received...")
    server_reply = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
    append_status(server_reply)
End Sub
 

dim

Member
Licensed User
Longtime User
Hmmmm,
adding CRLF after
command_1 = "TEST"
solved the problem...

For anyone else having the same problem, just use :
command_1 = "TEST" & CRLF
instead of
command_1 = "TEST"
 
Upvote 0
Top