iOS Question UDP_PacketArrived fires only one time

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi, i already did a post on UDP, where Erel suggest me to do a simper project (i copied the one in the guide) but i doensn't work
I send a packet when pressing a button, my server always respond with another packet saying "OK", the problem is that the sub "PacketArrived" fires only the first time, if i send another packet, it arrives to the server, it respond, but nothing fires in b4i...
this is the code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Dim UDPSocket1 As UDPSocket
    Private btnSend As Button
    Private txt1 As TextField
End Sub

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)
    
    UDPSocket1.Initialize("UDP", 0, 8000)
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Msgbox("Message received: " & msg, "")
End Sub

Sub btnSend_Click
    Dim Packet As UDPPacket
    Dim data() As Byte
    data = txt1.Text.GetBytes("UTF8")
    Packet.Initialize(data, "--.---.--.---", -----)
    UDPSocket1.Send(Packet)
End Sub


Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested with:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Dim UDPSocket1 As UDPSocket
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    NavControl.ShowPage(Page1)
    Dim server As ServerSocket 'ignore
    Log(server.GetMyWifiIp)
    UDPSocket1.Initialize("UDP", 14000, 8000)
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Log("Message received: " & msg)
End Sub

And B4J code:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim socket As UDPSocket
    socket.Initialize("socket", 11111, 8192)
    Do While True
        Dim udp As UDPPacket
        udp.Initialize(DateTime.Time(DateTime.Now).GetBytes("utf8"), "192.168.0.107", 14000)
        socket.Send(udp)
        Sleep(1000)
    Loop
End Sub

B4i logs:
Application_Start
192.168.0.107
Application_Active
Message received: 16:07:05
Message received: 16:07:06
Message received: 16:07:07
Message received: 16:07:08
Message received: 16:07:09
Message received: 16:07:10
Message received: 16:07:11
Message received: 16:07:12
Message received: 16:07:13
Message received: 16:07:14
Message received: 16:07:15
Message received: 16:07:16
Message received: 16:07:17
...
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Note that @Mike1970 previously (other thread) mentioned:
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
He did not specify iOS version running on iPhone 6
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
I've tested with:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Dim UDPSocket1 As UDPSocket
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    NavControl.ShowPage(Page1)
    Dim server As ServerSocket 'ignore
    Log(server.GetMyWifiIp)
    UDPSocket1.Initialize("UDP", 14000, 8000)
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Log("Message received: " & msg)
End Sub

And B4J code:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim socket As UDPSocket
    socket.Initialize("socket", 11111, 8192)
    Do While True
        Dim udp As UDPPacket
        udp.Initialize(DateTime.Time(DateTime.Now).GetBytes("utf8"), "192.168.0.107", 14000)
        socket.Send(udp)
        Sleep(1000)
    Loop
End Sub

B4i logs:
Application_Start
192.168.0.107
Application_Active
Message received: 16:07:05
Message received: 16:07:06
Message received: 16:07:07
Message received: 16:07:08
Message received: 16:07:09
Message received: 16:07:10
Message received: 16:07:11
Message received: 16:07:12
Message received: 16:07:13
Message received: 16:07:14
Message received: 16:07:15
Message received: 16:07:16
Message received: 16:07:17
...

Ok this script works. but doesn't do exaclty what i need, the phone Sends AND receives, the server too.
So i changed you script to make it like my python script on the server.


B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim socket As UDPSocket
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
   
    socket.Initialize("socket", 11111, 8192)
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub socket_PacketArrived (Packet As UDPPacket)
    Log($"Host: ${Packet.Host}" Porta:${Packet.Port}"$)
   
    Dim udp As UDPPacket
    udp.Initialize(DateTime.Time(DateTime.Now).GetBytes("utf8"), Packet.Host, Packet.Port)
    socket.Send(udp)
End Sub

Now when i send a message from the phone, it respond every time.

BUT

i tried to use internal IP also with my python script, and it magically start working as desired...
Now.. the problem is when using external ip, in this case even the B4J script does not work.

Solution: when i try the program in the same network of the server i've to use Internal IP, like you did.
meanwhile, when the client and the server are not in same wifi, i can continue to use the external ip.
That was the mistake... i was trying with external ip, in the same network
This does not explain why i was still able to receive even one single packet
šŸ¤”
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Now.. the problem is when using external ip, in this case even the B4J script does not work.
It is not a script it is a program.

Two things:
- You made many posts and never said that you were using an external IP address. We cannot guess such things and this just wastes the time of members who try to help you.
- The problem has nothing to do with any code. It is a network problem.

This does not explain why i was still able to receive even one single packet
Depending on the network configuration, when you send a packet the firewall can temporary allow incoming packets.
 
Upvote 0
Top