Android Question issue passing packet results to variables then calling on them

dualznz

Member
Licensed User
Longtime User
hi all
the application i am creating allows a user to query a game server which used udppackets that will then display the results.

i the query works fine with no problem.

BUT

when i try calling on the result which will then be displayed within my panels it just appears blank
i am kind of lost for words in how to fix it

here is code snippets of what i am doing

B4X:
Sub Globals
    Dim Conv As ByteConverter
    Dim Bytes(0) As Byte
    ' /////// PASS THE RETURNED PACKET TO THESE BELOW VARIABLES ///////
    Dim servername As String
    Dim passwd As String
    Dim racestatus As String
    Dim currentplayers As String
    Dim totalplayers As String
    Dim track As String
    Dim wear As String
    Dim real As String
    Dim length As String
    Dim port As String
    Dim flags As String
    ' /////// END ///////
 
End Sub

calling the results of the packet

B4X:
Sub UDP_PacketArrived (packet As UDPPacket)
    Dim msg As String
    Dim newbyte() As Byte
 
    msg = BytesToString(packet.Data, packet.Offset, packet.length, "ASCII")
    servername = msg.SubString2(20, 45).Trim
    ' check to see if a password is required on the server
    passwd = Byte2toint(packet.Data, 55)
    If passwd = 1 Then
        passwd = "Required"
    Else
        passwd = "Not Required"
    End If
    port = Byte2toint(packet.Data, 52).Trim
    track = msg.SubString2(69, 84).Trim
End Sub

displaying within my new loop
THIS IS WHERE THE INFORMATION IS NOT BEING OUTPUTTED
B4X:
Sub getmainlist
    CsrServerList1 = SQLServerList.ExecQuery("SELECT * FROM pub_serverlist")
 
    ' initilise the scrollview that will cover the entire activity
    svcList.Initialize(50)
    ' the scrollview takes tge whole screen gets added to the activity
    Activity.AddView(svcList,0,100dip,100%x,80%y)
 
    ' initilise the images
    srvOnline.Initialize(File.DirAssets, "flag_green.png")
    srvOffline.Initialize(File.DirAssets, "flag_red.png")
 
    ' create the panel that holds all other panels
    Dim PanelGreen As Panel
    ' attach panel green to the scrollview and give it a color
    PanelGreen = svcList.Panel
    PanelGreen.Color = Colors.Green
 
    ' the panel that holds each item
    Dim itemPanel As Panel
    ' set the top and the height of the very first item - set to 0 so its at the very top of the scrollview
    Dim itemPanelTop, itemPanelHeight As Int
    itemPanelTop = 0
    itemPanelHeight = 90dip
 
    ' the very heart of the sub. Iterate through each row of data and creates each entry wirh labels, text and so forth
    For i=0 To CsrServerList1.RowCount - 1
        ' position 1, 2, 3 and so forth
        CsrServerList1.Position = i
        ' get the ID of each entry as it will be added to the tag od each view (label, image etc)
        Dim ID As Int
        ID = CsrServerList1.GetInt("id")
     
        ' itemPanel Panel that holds each Item
        itemPanel.Initialize("itemPanel")
        itemPanel.Tag = ID
        PanelGreen.AddView(itemPanel,0,itemPanelTop,svcList.Width,itemPanelHeight)
        'itemPanel.Color = Colors.DarkGray ' can use a single color
        ' make the itemPanel Panel into a gradient to hold more that one color
        Dim gd1 As GradientDrawable
        Dim cols1(2) As Int
        gd1.Initialize("TOP_BOTTOM", cols1)
        gd1.CornerRadius = 20
        itemPanel.Background = gd1
     
        cols1(0) = Colors.LightGray
        cols1(1) = Colors.DarkGray
     
        Dim ip As String = servername
 
    ' SERVER IP ADDRESS
        Dim lblServerName As Label
        lblServerName.Initialize("lblServerName")
        itemPanel.AddView(lblServerName,80dip,0dip,240dip,40dip)
        lblServerName.TextColor = Colors.White
        lblServerName.TextSize = 24
        lblServerName.Tag = ID
        lblServerName.Text = ip
     
        ' very important gives the top of the next itempnl as it stacks down the pnlgreen
        itemPanelTop = itemPanelTop + itemPanelHeight + 1dip
        Log("ItemPnl Top " & itemPanelTop)
    Next
    ' the final height of the green panel is the height of the last item and its top.
    PanelGreen.Height = itemPanelTop
    ' close the cursor to free up resources
    CsrServerList1.Close
End Sub

im not sure why i cant return the results what i am looking for within the panels
the panels show but no data is showing

i am trying to display multiple servers in a list i tryed adding everything into 2 subs but it just wont work i think its killing its self when it goes through each server and it overrides the int variables but i am unsure on how to correct this

below is how i initate hte udp packet

BELOW the udp try catch i had the rest of the information to poplate the views but it did not make a difference

which i replaces with "getmainlist" hich starts a new query

B4X:
Sub populatescrollviewlist
    CsrServerList = SQLServerList.ExecQuery("SELECT * FROM pub_serverlist")
    ' the very heart of the sub. Iterate through each row of data and creates each entry wirh labels, text and so forth
    For i=0 To CsrServerList.RowCount - 1
        ' position 1, 2, 3 and so forth
        CsrServerList.Position = i
        ' get the ID of each entry as it will be added to the tag od each view (label, image etc)
        Dim ID As Int
        ID = CsrServerList.GetInt("id")
        ' ////////// UDP PACKET START //////////
        Dim ip, qportb4 As String
        Dim qportaft As Int
        ip = CsrServerList.GetString("serverip")
        qportb4 = CsrServerList.GetString("queryport")
        qportaft = qportb4
        Try
            Dim packet As UDPPacket
            Dim Data() As Byte
            Data = "server track arena".GetBytes("ASCII")
            packet.Initialize(Data,ip,qportb4)
            UdpClient.Send(packet)
        Catch
            Msgbox("something wnet wrong", "packet error found")
            Log(LastException)
        End Try
        ' ////////// UDP PACKET STOP ///////////
        getmainlist
    Next
    CsrServerList.Close
End Sub
 
Last edited:

dualznz

Member
Licensed User
Longtime User
this is the full populatescrollview sub that i just updated but no changes
B4X:
Sub populatescrollviewlist
    CsrServerList = SQLServerList.ExecQuery("SELECT * FROM pub_serverlist")
    ' initilise the scrollview that will cover the entire activity
    svcList.Initialize(50)
    ' the scrollview takes tge whole screen gets added to the activity
    Activity.AddView(svcList,0,100dip,100%x,80%y)
   
    ' initilise the images
    srvOnline.Initialize(File.DirAssets, "flag_green.png")
    srvOffline.Initialize(File.DirAssets, "flag_red.png")
   
    ' create the panel that holds all other panels
    Dim PanelGreen As Panel
    ' attach panel green to the scrollview and give it a color
    PanelGreen = svcList.Panel
    PanelGreen.Color = Colors.Green
   
    ' the panel that holds each item
    Dim itemPanel As Panel
    ' set the top and the height of the very first item - set to 0 so its at the very top of the scrollview
    Dim itemPanelTop, itemPanelHeight As Int
    itemPanelTop = 0
    itemPanelHeight = 90dip
   
    ' the very heart of the sub. Iterate through each row of data and creates each entry wirh labels, text and so forth
    ' the very heart of the sub. Iterate through each row of data and creates each entry wirh labels, text and so forth
    For i=0 To CsrServerList.RowCount - 1
        ' position 1, 2, 3 and so forth
        CsrServerList.Position = i
        ' get the ID of each entry as it will be added to the tag od each view (label, image etc)
        Dim ID As Int
        ID = CsrServerList.GetInt("id")
        ' ////////// UDP PACKET START //////////
        Dim ip, qportb4 As String
        Dim qportaft As Int
        ip = CsrServerList.GetString("serverip")
        qportb4 = CsrServerList.GetString("queryport")
        qportaft = qportb4
        Try
            Dim packet As UDPPacket
            Dim Data() As Byte
            Data = "server track arena".GetBytes("ASCII")
            packet.Initialize(Data,ip,qportb4)
            UdpClient.Send(packet)
        Catch
            Msgbox("something wnet wrong", "packet error found")
            Log(LastException)
        End Try
        ' ////////// UDP PACKET STOP ///////////
       
        ' itemPanel Panel that holds each Item
        itemPanel.Initialize("itemPanel")
        itemPanel.Tag = ID
        PanelGreen.AddView(itemPanel,0,itemPanelTop,svcList.Width,itemPanelHeight)
        'itemPanel.Color = Colors.DarkGray ' can use a single color
        ' make the itemPanel Panel into a gradient to hold more that one color
        Dim gd1 As GradientDrawable
        Dim cols1(2) As Int
        gd1.Initialize("TOP_BOTTOM", cols1)
        gd1.CornerRadius = 20
        itemPanel.Background = gd1
       
        cols1(0) = Colors.LightGray
        cols1(1) = Colors.DarkGray
       
        Dim ip As String = servername
   
    ' SERVER IP ADDRESS
        Dim lblServerName As Label
        lblServerName.Initialize("lblServerName")
        itemPanel.AddView(lblServerName,80dip,0dip,240dip,40dip)
        lblServerName.TextColor = Colors.White
        lblServerName.TextSize = 24
        lblServerName.Tag = ID
        lblServerName.Text = ip
       
        ' very important gives the top of the next itempnl as it stacks down the pnlgreen
        itemPanelTop = itemPanelTop + itemPanelHeight + 1dip
        Log("ItemPnl Top " & itemPanelTop)
    Next
    ' the final height of the green panel is the height of the last item and its top.
    PanelGreen.Height = itemPanelTop
    ' close the cursor to free up resources
    CsrServerList.Close
End Sub
 
Upvote 0

dualznz

Member
Licensed User
Longtime User
yes the packet works find from the UDP and it produces results but i am then trying to populate the panels with information as there is more than one server.

if there is only 1 server in the list it works fine as soon as i add more than 1 server it empties the fields.
i think the variables are being overridden to fast for it to store it in each loop statement.

if i was able to delay the UDP in each send then it would have enough time to store it within the loop
 
Upvote 0

dualznz

Member
Licensed User
Longtime User
hrmm ok i have tried everything to get it to work

here is the code which looks for the servers, and displayes them

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim SQLServerList As SQL
    Dim CsrServerList As Cursor
    Dim CsrServerList1 As Cursor
    Dim CsrResults As Cursor
    Dim CsrResults1 As Cursor
    Dim idtag As Int
    Dim sortorder As Int
    Dim httpC As HttpClient
    ' /////// UDP ///////
    Dim UdpClient As UDPSocket
    ' /////// END ///////
    ' /////// PASS THE RETURNED PACKET TO THESE BELOW VARIABLES ///////
    Dim servername As String
    Dim passwd As String
    Dim racestatus As String
    Dim currentplayers As String
    Dim totalplayers As String
    Dim track As String
    Dim wear As String
    Dim real As String
    Dim length As String
    Dim port As String
    Dim flags As String
    ' /////// END ///////
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim serverimg As Bitmap
    Dim svcList As ScrollView
    Dim Bmp As Bitmap
    Dim srvOnline As Bitmap
    Dim srvOffline As Bitmap
    Dim reader As TextReader
    ' /////// UDP ///////
    Dim Conv As ByteConverter
    Dim Bytes(0) As Byte

    Dim queryid As Int



End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("serverlist")

    If File.Exists(File.DirInternal, "hirfandroid.sql") = False Then
        File.Copy(File.DirAssets, "hirfandroid.sql", File.DirInternal, "hirfandroid.sql") ' copy the file to the assets folder so it can be used within the program while its running
    End If

    ' initialize the databaseS
    If SQLServerList.IsInitialized = False Then
        SQLServerList.Initialize(File.DirInternal, "hirfandroid.sql", False)
    End If

    ' start the httpC request
    httpC.Initialize("httpC")

    ' initialise a Bmp to hold the images
    Bmp.Initialize(File.DirAssets, "wheel.png")


    UdpClient.Initialize("udp",0,8000)


    ' start the download server list routine
    downloadlist

End Sub

Sub downloadlist
    ' input the required information to talk to the remote server in order to download the latest server list
    Dim req As HttpRequest
    req.InitializeGet("*************")
    httpC.Execute(req, 1)
End Sub

Sub httpC_ResponseSuccess(Response As HttpResponse, TaskId As Int)
    Try
        Dim result As String
        result = Response.GetString("UTF8")
        result = result.Replace("_", " ")
        File.WriteString(File.DirInternalCache, "datapull.txt", result)
        reader.Initialize(File.OpenInput(File.DirInternalCache, "datapull.txt"))
 
        ' External database import and loop through the data
        Dim id As String
        id = reader.ReadLine
 
        Dim i As Int :i = 0
        Do While id <> Null
            i = i+1
            Dim idfromdb As String
            idfromdb = reader.readline
     
            Dim serverip As String
            serverip = reader.ReadLine
     
            Dim serverqueryport As String
            serverqueryport = reader.ReadLine
     
            Dim isalive As String
            isalive = reader.ReadLine
     
            id = reader.ReadLine
     
            ' insert data into the internal database
 
        ' check to see if the information is already in the database table
        CsrServerList1 = SQLServerList.ExecQuery("SELECT id, is_alive FROM public_list WHERE serverip='" & serverip & "' AND queryport='" & serverqueryport & "'")
        If CsrServerList1.RowCount > 0 Then
            For i = 0 To CsrServerList1.RowCount - 1
                CsrServerList1.Position = i
                Dim perjobid As Int
                perjobid = CsrServerList1.GetInt("id")
         
                Dim perjobstatus As String
                perjobstatus = CsrServerList1.GetString("is_alive")
                ' check to see if the customer is already in the database
                If perjobstatus == isalive Then
                    ' skip customer and move to next one
                    'Msgbox(customername & " is already in the database, skipping this", "row found!")
                Else
                    ' update customer information
                    SQLServerList.ExecNonQuery("UPDATE public_list SET is_alive='" & isalive & "' WHERE id='" & perjobid & "'")
                    'Msgbox(customername & " data has been updated to the status of " & statusfk, "updated existing row") ' testing
                End If
            Next
        ' if customer is not in the databse then insert new record
        Else If CsrServerList1.RowCount = 0 Then
            ' insert into the database table
            SQLServerList.ExecNonQuery("INSERT INTO public_list(serverip, queryport, is_alive) VALUES ('" & serverip & "', '" & serverqueryport & "', '" & isalive & "')")
            'Msgbox(customername & " has been added", "customer added") ' testing
        End If
        CsrServerList1.Close ' close sql query
        File.Delete(File.DirInternalCache, "datapull.txt") ' delete the text file
    Loop
    reader.Close ' close the text reader

    ' check the database to see if there is any empty fields then delete them
    Dim rowid2 As String
        CsrServerList1 = SQLServerList.ExecQuery("SELECT id FROM public_list WHERE serverip='' OR queryport=''")
        If CsrServerList1.RowCount > 0 Then
            For i = 0 To CsrServerList1.RowCount - 1
            CsrServerList1.Position = i
            rowid2 = CsrServerList1.GetString("id")
            SQLServerList.ExecNonQuery("DELETE FROM public_list WHERE id='" & rowid2 & "'")
            'Msgbox("Empty Row Deleted", "Removed") 'testing
            Next
        End If
        CsrServerList1.Close ' close the sql query
    Catch
        Log(LastException)
    End Try

    populatescrollviewlist ' start the population onto the device screen
End Sub

Sub httpC_ResponseError(Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    'lblStatus.Text = "login ok, cannot pass information to remote server!"
    ProgressDialogHide
End Sub

Sub populatescrollviewlist
    CsrServerList = SQLServerList.ExecQuery("SELECT * FROM public_list")
    For i=0 To CsrServerList.RowCount - 1
        ' position 1, 2, 3 and so forth
        CsrServerList.Position = i
        ' get the ID of each entry as it will be added to the tag od each view (label, image etc)
        Dim ID As Int
        ID = CsrServerList.GetInt("id")
        ' ////////// UDP PACKET START //////////
        Dim ip, qportb4 As String
        Dim qportaft As Int
        ip = CsrServerList.GetString("serverip")
        qportb4 = CsrServerList.GetString("queryport")
        qportaft = qportb4
        Try
            queryid = ID
            Dim packet As UDPPacket
            Dim Data() As Byte
            Data = "server track arena".GetBytes("ASCII")
            packet.Initialize(Data,ip,qportb4)
            UdpClient.Send(packet)
        Catch
            Msgbox("something went wrong", "packet error found")
            Log(LastException)
        End Try
        ' ////////// UDP PACKET STOP ///////////
    Next
    mainlist
    ' close the cursor to free up resources
    CsrServerList.Close
End Sub

Sub Byte4toint (buffer() As Byte, Offset As Int)
    Dim Byte4 As Int
    Byte4 = buffer(Offset)
    Byte4 = Bit.AND(Byte4,0x7F)
    Return Byte4
End Sub

Sub Byte2toint (buffer() As Byte, Offset As Int)
    Dim Byte2 As Int
    Byte2 = buffer(Offset)
    Byte2 = Bit.AND(Byte2,0x7FF)
    Return Byte2
End Sub

Sub UDP_PacketArrived (packet As UDPPacket)
    Dim msg As String
    Dim newbyte() As Byte
    Dim relid As Int = id

    msg = BytesToString(packet.Data, packet.Offset, packet.length, "ASCII")
    servername = msg.SubString2(20, 45).Trim
    ' check to see if a password is required on the server
    passwd = Byte2toint(packet.Data, 55)
    If passwd = 1 Then
        passwd = "Required"
    Else
        passwd = "Not Required"
    End If
    port = Byte2toint(packet.Data, 52).Trim
    track = msg.SubString2(69, 84).Trim
    wear = msg.SubString2(131, 133).Trim
    real = msg.SubString2(86, 96).Trim
    length = msg.SubString2(111, 118).Trim
    flags = msg.SubString2(98, 103).Trim
    racestatus = Byte4toint(packet.Data, 64).Trim
    If racestatus = 0 Then
        racestatus = "Open"
    Else If racestatus = 2 Then
        racestatus = "Ready"
    Else If racestatus = 3 Then
        racestatus = "Syncronising"
    Else If racestatus = 4 Then
        racestatus = "Racing"
    End If
    currentplayers = Byte4toint(packet.Data, 208).Trim
    totalplayers = Byte4toint(packet.Data, 16).Trim
End Sub

Sub mainlist
    CsrResults = SQLServerList.ExecQuery("SELECT * FROM public_list")
    ' initilise the scrollview that will cover the entire activity
    svcList.Initialize(50)
    ' the scrollview takes tge whole screen gets added to the activity
    Activity.AddView(svcList,0,100dip,100%x,80%y)

    ' initilise the images
    srvOnline.Initialize(File.DirAssets, "flag_green.png")
    srvOffline.Initialize(File.DirAssets, "flag_red.png")

    ' create the panel that holds all other panels
    Dim PanelGreen As Panel
    ' attach panel green to the scrollview and give it a color
    PanelGreen = svcList.Panel
    PanelGreen.Color = Colors.Green

    ' the panel that holds each item
    Dim itemPanel As Panel
    ' set the top and the height of the very first item - set to 0 so its at the very top of the scrollview
    Dim itemPanelTop, itemPanelHeight As Int
    itemPanelTop = 0
    itemPanelHeight = 90dip

    ' the very heart of the sub. Iterate through each row of data and creates each entry wirh labels, text and so forth
    For i=0 To CsrResults.RowCount - 1
        ' position 1, 2, 3 and so forth
        CsrResults.Position = i
        ' get the ID of each entry as it will be added to the tag od each view (label, image etc)
        Dim ID As Int
        ID = CsrResults.GetInt("id")
 
        ' itemPanel Panel that holds each Item
        itemPanel.Initialize("itemPanel")
        itemPanel.Tag = ID
        PanelGreen.AddView(itemPanel,0,itemPanelTop,svcList.Width,itemPanelHeight)
        'itemPanel.Color = Colors.DarkGray ' can use a single color
        ' make the itemPanel Panel into a gradient to hold more that one color
        Dim gd1 As GradientDrawable
        Dim cols1(2) As Int
        gd1.Initialize("TOP_BOTTOM", cols1)
        gd1.CornerRadius = 20
        itemPanel.Background = gd1
 
        cols1(0) = Colors.LightGray
        cols1(1) = Colors.DarkGray

    ' SERVER IP ADDRESS
        Dim lblServerName As Label
        lblServerName.Initialize("lblServerName")
        itemPanel.AddView(lblServerName,80dip,0dip,240dip,40dip)
        lblServerName.TextColor = Colors.White
        lblServerName.TextSize = 24
        lblServerName.Tag = ID
        'lblServerName.Text = CsrResults.GetString("id")
 
        ' very important gives the top of the next itempnl as it stacks down the pnlgreen
        itemPanelTop = itemPanelTop + itemPanelHeight + 1dip
        Log("ItemPnl Top " & itemPanelTop)
    Next
    ' the final height of the green panel is the height of the last item and its top.
    PanelGreen.Height = itemPanelTop
    ' close the cursor to free up resources
    CsrResults.Close
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

If i uncomment out lblServerName.Text = CsrResults.GetString("id") then it will display the id in each of the panels which is fine but if i try say servername it will not, i have just moved the varibales from globals to process globals to see if that would work.

also at the end of the udp i tried to update the databse by passing hte id through to it but that would only update 1 row and not both.

link to image http://i1228.photobucket.com/albums/ee450/dualznz/Screenshot_2013-08-19-08-20-55.png

this above image is the result from the database which means its working but i am sending the IP addresses and the port through to the UDP which works fine but then i am trying to show the outputted information for each server in their individual panels

servername instead of serverip

maybe its the way that b4a opeates or something, if i write the application in likes of C# or VB.net i can show all servers in the list and access each of them.

so nay light on how i can go about doing this would help i have exhausted the ways that i know of.

cheers
 
Last edited:
Upvote 0

dualznz

Member
Licensed User
Longtime User
the problem lyes with passing the variables from each of the server within the UDP then displaying the output into the panels as the udp cycles through the servers quite fast.

the HTTP only grabs the server ip and query port and thats it, that part is fine after that i am doing a query to the internal DB which is running within the for next loop which the UDP is contained within thats where the issue is as i even tried passing the ROWID to the udp so then i could update the internal DB but it was always only listing the last row in the db from the device.
 
Upvote 0
Top