Android Question Socket: How to receve data after sending...

Discussion in 'Android Questions' started by Vinians2006, Jun 11, 2015.

  1. Vinians2006

    Vinians2006 Member Licensed User

    I have sucessfully sent a string to my Java Desktop application from my B4A app.
    I'm using this code:
    Code:
    Sub btnEnviar_Click
       cliente.Initialize(
    "cliente")
       cliente.Connect(
    "192.168.0.101"900010000)
    End Sub

    Sub cliente_Connected (Successful As Boolean)
       
    If (Successful) Then
         
    Dim out As OutputStream
         
    Dim inp As InputStream
         
    Dim len As Int
         
    Dim buffer(255As Byte
         
    Dim ret As String
       
         out = cliente.OutputStream
         inp = cliente.InputStream
       
         
    Dim texto As String
         texto = edtString.Text
         texto = 
    NumberFormat2(texto.Length,4,0,0,False) & texto
         len = texto.Length
         
    ' Send data (working)
         out.WriteBytes(texto.GetBytes("ASCII"), 0, len)
       
         
    ' Get response (not working)
         ret = ""
         
    If (inp.BytesAvailable > 0Then
           inp.ReadBytes(buffer, 
    0, buffer.length)
           ret = 
    BytesToString(buffer, 0, buffer.Length, "ASCII")
         
    End If
         edtString.Text = ret
         cliente.close
       
    Else
         
    ToastMessageShow("Erro conectando"True)
       
    End If
    End Sub
    My Java server, returns the "OK" string, but I didnt found the name of the event that I need to receive the server response, so I placed directly after sending information. I know its wrong, but whats the name of the event ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    lemonisdead and Peter Simpson like this.
  3. Vinians2006

    Vinians2006 Member Licensed User

    Thanks Erel. But, if I want to use this way how can I should proceed to use it without using AsyncStream?????
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can't. Android doesn't allow you to block the main thread. You will an exception when you run this code in Release mode (on Android 4+).
     
  5. Vinians2006

    Vinians2006 Member Licensed User

    Ok, but using this library I can send individual bytes over TCP/IP (same network) ?
    And where do I find this library ? The link on then tutorial is broken...
    Thanks!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Which link is broken?

    It is part of RandomAccessFile library.

    Yes. Please read the tutorials.
     
Loading...