B4J Question UDP Problem

Jerez

Active Member
Licensed User
Longtime User
Hi,

i've tried to do a little program that when detect a request then send a string to device.

This app can receive the data but don't send. How can i fix it?

But, if i execute my equivalent version in C# works like a charm. Then thats not a network problem.

I'm using:
B4X:
C:\Program Files (x86)\Java\jdk1.7.0_71\bin\javac.exe
B4X:
B4J 3.50

JavaObject 2.02
jCore 3.50
jFX 3.50
jNetwork 1.11
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim UDPSocket1 As UDPSocket
    Dim jip As ServerSocket 'ignore
  
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    'MainForm.Show
  
    UDPSocket1.Initialize("UDP", 8888, 8000)
  
    Log("Local IP:" & jip.GetMyIP)
  
End Sub


Sub UDP_PacketArrived (Packet As UDPPacket)
  
    Log ("Request from:" & Packet.HostAddress)
      
    If Packet.HostAddress <> jip.GetMyIP Then 
      
        Log("Send response: " & Packet.HostAddress)
      
        Dim Packet As UDPPacket
      
        Dim dest As String
        Dim data() As Byte
      
        data = "CONTROLLER".GetBytes("UTF8")
      
        dest = "255.255.255.255"
      
        Packet.Initialize(data, dest, 8888)
      
        UDPSocket1.Send(Packet) '<-- this line do nothing...
      
    End If     
  
End Sub

Please advise
 

Attachments

  • upd_listener.zip
    961 bytes · Views: 264
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
I just tested equivalent code and it worked. I even changed my Java version to 7u71 to make sure it wasn't a Java version issue. It works. I'm not sure what is going wrong on your end but if I had to guess, I'd say your firewall/anti-virus wasn't set up to allow outgoing UDP packets to pass.

As a side note, you're using the variable name Packet to refer to two different UDPPacket objects in your UDP_PacketArrived sub. It will still compile and run but it's not the greatest of programming practices.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Pls find attached an example of Client-to-Client communication using UDP.
Start from the Object folder client1 followed by client2.
In the dialog, left are the incoming messages and right the text to sent.

Have been using this simple solution to communicate ("chatting") between PC's while testing in different locations.
 

Attachments

  • B4JHowToUDPc2c.zip
    475.8 KB · Views: 299
Upvote 0

Jerez

Active Member
Licensed User
Longtime User
Try to change dest with Packet.HostAddress.

thanks Erel, it worked, but 8 of 10 times... maybe packets are lost?

B4X:
Log ("Request from:" & Packet.HostAddress)
  
    Dim remoteAddress As String = Packet.HostAddress
      
    If Packet.HostAddress <> jip.GetMyIP Then  
      
        Log("Send response: " & Packet.HostAddress)
      
        Dim response As UDPPacket
      
        Dim dest As String
        Dim data() As Byte
      
        data = "CONTROLLER_SMARTM2".GetBytes("UTF8")
      
        dest =  remoteAddress '"255.255.255.255"
      
        response.Initialize(data, dest, 8888)
      
      
        UDPSocket1.Send(response)
      
    End If
 
Upvote 0

Jerez

Active Member
Licensed User
Longtime User
UDP communication is not 100% reliable (by definition). Though 20% failure sounds a bit too much for a local network.

Thanks! ok, this code works as Chromecast... it's possible to replicate this in TCP? I mean send a string from an android device to 255.255.255.255 port 8888 waiting to my server answer the request?
 
Upvote 0

Jerez

Active Member
Licensed User
Longtime User
There is no multicast address in TCP communication. If you know the other device ip address then you can use TCP.

Thanks! you saved my time!
 
Upvote 0
Top