iOS Question iNetwork UDP not working

tman

Member
Licensed User
Longtime User
There seems to be a problem with sending UDP Packets. My app sends the same packet every second using a timer. However when I run it only one packet gets sent. I made a simple test app and I get the same results.I am using B4i ver. 4.01 Please help.

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: UDP Test
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 7
#End Region

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 Timer1 As Timer
    Dim bytes1() As Byte = Array As Byte (0xFE, 0x02, 0x01,0xfe,0x02)
    Dim UDPSocket1 As UDPSocket
    Dim Packet1 As UDPPacket
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.Color = Colors.White
    NavControl.ShowPage(Page1)
    Timer1.Initialize ("Timer1",1200)
    Timer1.Enabled = True
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
  
End Sub

Private Sub Application_Background
  
End Sub

Sub Timer1_Tick
    Dim UDPSocket1 As UDPSocket
    UDPSocket1.Initialize("UDP1",2107,180) 'event name, port, buffer)
    Packet1.Initialize(bytes1, "192.168.16.254",2107)
    UDPSocket1.Send(Packet1)

  
  
End Sub
 
Last edited:

tman

Member
Licensed User
Longtime User
Don't create a new UDPSocket for each packet. Create a single one and reuse it.
Erel,
I tried that and I get the same results - it sends the packet only 1 time. (It should send the packet every time the Timer ticks) My app runs perfectly in B4A and I was porting my code over to B4i when I ran into this issue. I also tried to create the socket once and reuse and still no result. I tried to close the socket each time and then reinitialize and it would not send the packet at all. Here is my test code where I create the packet once and reuse:

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: UDP Test
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 7
    #ProvisionFile: Distrib_Beta_Testers.mobileprovision
    #CertificateFile: ios_distribution.cer
#End Region

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 Timer1 As Timer
    Dim bytes1() As Byte = Array As Byte (0xFE, 0x02, 0x01,0xfe,0x02)
    Dim UDPSocket1 As UDPSocket
    Dim Packet1 As UDPPacket
   
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.Color = Colors.White
    NavControl.ShowPage(Page1)
    Packet1.Initialize(bytes1, "192.168.16.254",2107)
    Timer1.Initialize ("Timer1",1200)
    Timer1.Enabled = True
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
   
End Sub

Private Sub Application_Background
   
End Sub

Sub Timer1_Tick
    Dim UDPSocket1 As UDPSocket
    UDPSocket1.Initialize("UDP1",2107,180) 'event name, port 0 is auto, buffer)
    UDPSocket1.Send(Packet1)

   
   
End Sub
 
Upvote 0
Top