iOS Question UDP sending error data in Release mode

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, i'm trying to use UDP to send packets, but i'm getting some errors.
I tried this code on an iPhoneXR and it works in debug and also in release(last os version)... i tried the same code on an iPhone6 but when it is in release it doesn't work

I need to send a packet, and then wait for the "OK" answer, if the "OK" doens't arrive OR the timer ticks, it send again a packet with the same data.
On iPhone6 it stays stuck on packet "0".

B4X:
Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.LoadLayout("1")
    NavControl.ShowPage(Page1)
   
    tmr.Initialize("tmr", 1000)
    'UDPSocket1.Initialize("UDP", 0, 2048)
End Sub

Sub UDP_PacketArrived(Packet As UDPPacket)
   
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
   
    TextView1.Text = msg & CRLF & TextView1.Text
    TextView1.ScrollTo(0)
   
    If msg == "OK" Then
        res = "OK"
    End If
End Sub

Sub Button1_LongClick
    hud1.ToastMessageShow("Test invio pacchetti inziato", False)
    Dim i As Int = 0
    Dim l As Int = 0
    Dim t As Long = DateTime.Now
    Page1.ResignFocus
    TextView1.Text = ""
   
    Do While (i <= 99) And (tmr.Enabled == False)
        TextView1.Text = i & CRLF & TextView1.Text
        TextView1.ScrollTo(0)
        If UDPSocket1.IsInitialized Then UDPSocket1.Close
        Sleep(400)
        UDPSocket1.Initialize("UDP", 0, 2048)
       
        Dim Packet As UDPPacket
        Dim data() As Byte
        Dim convstring As String = $"${i}"$
        data = convstring.GetBytes("UTF8")
       
        Packet.Initialize(data, "--.---.--.---", 12000)
        UDPSocket1.Send(Packet)
        res = ""
        tmr.Enabled = True
   
        Do While (tmr.Enabled == True) And (res == "")
            Sleep(0)
        Loop
       
        If res == "OK" Then
            i=i+1
        Else
            l=l+1
        End If
       
        tmr.Enabled = False
        Sleep(0)
    Loop
   
    Msgbox("Pacchetti inviati:"&i&CRLF&"Pacchetti persi: "&l&CRLF&"Tempo impiegato: "&DateTime.GetSecond(DateTime.Now - t)&"sec", "Resoconto")
    UDPSocket1.Close
End Sub

Sub tmr_Tick
    tmr.Enabled = False
End Sub

Sub Page1_Click
    Page1.ResignFocus
End Sub


Thanks in advance!
 

OliverA

Expert
Licensed User
Longtime User
If UDPSocket1.IsInitialized Then UDPSocket1.Close Sleep(400) UDPSocket1.Initialize("UDP", 0, 2048)
Why are you doing this inside the loop? Actually, create the UDPSocket1 in Application_Start and then check before the loop if it is Initialized. If it is NOT initialized, then create (DIM) a new UDPSocket, initialize it and then assign it to UDPSocket1.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Why are you doing this inside the loop? Actually, create the UDPSocket1 in Application_Start and then check before the loop if it is Initialized. If it is NOT initialized, then create (DIM) a new UDPSocket, initialize it and then assign it to UDPSocket1.
I did like this because i noticed that if I initialize only one time the UDPsocket1 , the event “packet_received” fires only ONE time, even if i receive multiple packets.

example:
i send 10 udp packets from my server to the app, the event “packet_received” fires only for the first packet, the other 9 are lost.

so i decided to initialize It everytime i have to send something , so i can listen for an answer from the server
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
This is not needed and a mistake.
I know its a mistake initialize the udpsocket every time, but if i don't do like so, I'm not able to receive more than one packet, the "Packet_Received" sub fire only for the first one... is the only way I founded to get the sub work multiple time, but now is getting other errors... I don't understand what I've to do to get all the udp functions working
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
The complete code should be deleted. Nothing good will come out of it.

Start with a simple code that logs the incoming message and also don't set the port number to 0. If it is not working then post it.
I started from the one in the B4X Guide, i can try again to spot the problem before and make a new post
 
Upvote 0
Top