Android Question split data from vb.net to B4A

klingon467

Member
Licensed User
Longtime 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
B4X:
 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 - 1) As 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
B4X:
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
 

klingon467

Member
Licensed User
Longtime User
If you can I highly recommend you to use B4J to implement the server. It will be very simple to send any data you like between the two:
https://www.b4x.com/android/forum/threads/34612/#content

Can you post an example of a string and how you want to parse it?
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
B4X:
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
B4X:
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(9) As 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

B4X:
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
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
B4X:
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
 
Upvote 0

Thraka

Member
Licensed User
Longtime 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.
B4X:
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.

B4X:
opt = Split(text, "|").Reverse()

Now your array will appear backwards.
 
Upvote 0

klingon467

Member
Licensed User
Longtime 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.
i try it bu not work for me....

from client (B4A) connection

B4X:
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)

B4X:
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(9) As 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"

debug.jpg
 
Last edited:
Upvote 0

klingon467

Member
Licensed User
Longtime User
The issue here is different. | is a special regex character. You should use: Regex.Split("\|", ...)
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?

B4X:
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!
 
Upvote 0
Top