B4J Question UDP receive Data from mDNS

Peter Lewis

Active Member
Licensed User
Longtime User
HI All,

I am trying to receive the data from Multicast DNS . I see the data using Wireshark but I need to process the data. The IP for the multicast is 224.0.0.251 and port is 5353.

1733862497684.png

I have tried this code
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 200
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim UDPSocket1 As UDPSocket
    Private txtLogs As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    UDPSocket1.Initialize("UDP", 5353, 8000)
End Sub

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

Sub UpdateLogs(strLog As String)
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
    txtLogs.Text =  DateTime.Date(DateTime.now) & ": " & strLog & CRLF & txtLogs.Text
End Sub

But I am getting this error, Have tried rebooting , any Ideas ? Thank you

main._appstart (java line: 58)
java.net.BindException: Address already in use: Cannot bind
at java.base/java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.base/java.net.DualStackPlainDatagramSocketImpl.bind0(DualStackPlainDatagramSocketImpl.java:84)
at java.base/java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:131)
at java.base/java.net.DatagramSocket.bind(DatagramSocket.java:394)
at java.base/java.net.DatagramSocket.<init>(DatagramSocket.java:244)
at java.base/java.net.DatagramSocket.<init>(DatagramSocket.java:301)
at java.base/java.net.DatagramSocket.<init>(DatagramSocket.java:273)
at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket.Initialize(SocketWrapper.java:352)
at b4j.example.main._appstart(main.java:58)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:37)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

UPDATE: I tried using

B4X:
UDPSocket1.Initialize2("UDP", 5353, 8000,True,False)

and checked on the broadcast address and it is using the incorrect IP. I do not see any way of forcing the correct IP
 
Last edited:

walterf25

Expert
Licensed User
Longtime User
HI All,

I am trying to receive the data from Multicast DNS . I see the data using Wireshark but I need to process the data. The IP for the multicast is 224.0.0.251 and port is 5353.

View attachment 159407
I have tried this code
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 200
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim UDPSocket1 As UDPSocket
    Private txtLogs As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    UDPSocket1.Initialize("UDP", 5353, 8000)
End Sub

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

Sub UpdateLogs(strLog As String)
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
    txtLogs.Text =  DateTime.Date(DateTime.now) & ": " & strLog & CRLF & txtLogs.Text
End Sub

But I am getting this error, Have tried rebooting , any Ideas ? Thank you



UPDATE: I tried using

B4X:
UDPSocket1.Initialize2("UDP", 5353, 8000,True,False)

and checked on the broadcast address and it is using the incorrect IP. I do not see any way of forcing the correct IP
This means the address is being used, maybe by wireshark, try killing that instance.

Walter
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i'm wondering why you use multicast instead of broadcast. it involves some configuration that you do not show. in addition, you show a port not normally used for multicast.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
i'm wondering why you use multicast instead of broadcast. it involves some configuration that you do not show. in addition, you show a port not normally used for multicast.
It seems like I am really confused.

Setup Overview​

mDNS functions over both IPv4 and IPv6 network layers using multicast addresses and sending payloads over UDP port 5353.

Possibly this link to a Video will give you insight to what I am trying to do

mDNS explanation
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
multicast is like a subscription model. technically, it's more "efficient" than broadcast, but that would apply if you had hundreds of hosts on your network(s). both broadcast and multicast send out general packets to the network. with broadcast, everybody receives the messages. with multicast, only subscribers do. judging by the code you posted (and the post itself), my guess is you want multicast for some reason, but you appear not to have set it up fully. whether/how you would do that from b4a, i haven't looked into it (because i chose to use broadcast). so there's that. there is also the question i ask as to why you need multicast. broadcast might suit your purposes, plus it's easier to configure.
regarding the error you receive concerning the address in use, it speaks for itself and is possibly not related to the broadcast vs multicast question, unless you're trying to open the same port on the same machine more than once (at a time).
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
See if any other process is using that port:

(Windows) netstat -ano | find "5353"

(Linux) netstat -nap | grep 5353

Kill any other process that's using the same port.

If that does not work..... and this is a long-shot .... It looks like it could also be a network issue. If your computer is using multiple network adapters, a hotspot, VPN, etc. Try disabling them. Also, temporarily disable your firewall and see if that resolves it.
 
Last edited:
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
multicast is like a subscription model. technically, it's more "efficient" than broadcast, but that would apply if you had hundreds of hosts on your network(s). both broadcast and multicast send out general packets to the network. with broadcast, everybody receives the messages. with multicast, only subscribers do. judging by the code you posted (and the post itself), my guess is you want multicast for some reason, but you appear not to have set it up fully. whether/how you would do that from b4a, i haven't looked into it (because i chose to use broadcast). so there's that. there is also the question i ask as to why you need multicast. broadcast might suit your purposes, plus it's easier to configure.
regarding the error you receive concerning the address in use, it speaks for itself and is possibly not related to the broadcast vs multicast question, unless you're trying to open the same port on the same machine more than once (at a time).
The ESP32 I am connecting to only does mDNS. What you are discussing here is a subscribe basically like MQTT. My purpose is to have the mDNS discover from an item with NO IP address until it is assigned one via the DHCP server based on its MAC address. Looking at Wireshark in my first post , I can get all the information I want from that.

All I want B4J to is open up the same port that Wireshark is opening and receive the data. I cannot see why that is impossible.

To give you more perspective, I have 50 ESP's all connected via Ethernet. I need to know that they are on and working . At the moment I use a DHCP server with the IP related to MAC address. Then I have to ping each IP address wait for it to fail before I move on, this is a time consuming process. Using mDNS, I will get notification of each one within a few seconds that each one is alive. There are other reasons why ping is not the best method. in the Database I have over 200 MAC addresses I do not know which 50 will be the ones used.

The units used each day might be different. Once the units are determined as alive , We allocate new IP addresses to them via DHCP so that the Adresses are sequential in each column. ( This is required for the final product to use its map to project the correct data to each ESP depending on its position in the matrix.

1733942573501.png


is the setting on ESP
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
The ESP32 I am connecting to only does mDNS. What you are discussing here is a subscribe basically like MQTT. My purpose is to have the mDNS discover from an item with NO IP address until it is assigned one via the DHCP server based on its MAC address. Looking at Wireshark in my first post , I can get all the information I want from that.

All I want B4J to is open up the same port that Wireshark is opening and receive the data. I cannot see why that is impossible.

To give you more perspective, I have 50 ESP's all connected via Ethernet. I need to know that they are on and working . At the moment I use a DHCP server with the IP related to MAC address. Then I have to ping each IP address wait for it to fail before I move on, this is a time consuming process. Using mDNS, I will get notification of each one within a few seconds that each one is alive. There are other reasons why ping is not the best method. in the Database I have over 200 MAC addresses I do not know which 50 will be the ones used.

The units used each day might be different. Once the units are determined as alive , We allocate new IP addresses to them via DHCP so that the Adresses are sequential in each column. ( This is required for the final product to use its map to project the correct data to each ESP depending on its position in the matrix.

View attachment 159426

is the setting on ESP

Did you try:

netstat -ano | find "5353"

See Post #7 above
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
See if any other process is using that port:

(Windows) netstat -ano | find "5353"

(Linux) netstat -nap | grep 5353

Kill any other process that's using the same port.

If that does not work..... and this is a long-shot .... It looks like it could also be a network issue. If your computer is using multiple network adapters, a hotspot, VPN, etc. Try disabling them. Also, temporarily disable your firewall and see if that resolves it.
1733942701958.png


so I use Wifi Access so I can Anydesk into it, and a usb to Ethernet connecting to devices

if I use

B4X:
    UDPSocket1.Initialize2("UDP", 5353, 8000, True,False)
    Log(UDPSocket1.Port)
    Log(UDPSocket1.GetBroadcastAddress)

Then I get no error

5353
192.168.0.255

which is my main IP address.
What you are saying it to disable my main IP and I will be using the IP subnet of the adapter and that will change to 192.168.10.255

I want that IP to change to the Multicast address of 224.0.0.251 as Wireshark does get the data I want from that address and port 5353
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
if I use

UDPSocket1.Initialize2("UDP", 5353, 8000, True,False)
Log(UDPSocket1.Port)
Log(UDPSocket1.GetBroadcastAddress)[/CODE]

Then I get no error

OK... so by setting ReuseAddress to "true" you are allowing multiple processes to open the same port and you don't get the error. So that resolves the issue that you were having, right? If so, there is no need to do anything else.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
OK... so by setting ReuseAddress to "true" you are allowing multiple processes to open the same port and you don't get the error. So that resolves the issue that you were having, right? If so, there is no need to do anything else.
That is one hassle, The other problem which is the main issue is to specify the mDNS multicast address. That is the address with this port I need to monitor, If wireshark can pick it up, I should be able to as well
1733945662497.png


This is the data I want to capture into my app
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
That is one hassle, The other problem which is the main issue is to specify the mDNS multicast address. That is the address with this port I need to monitor, If wireshark can pick it up, I should be able to as well
View attachment 159430

This is the data I want to capture into my app

Aah... I see. As far as I know the jNetwork Library does not expose setting the BroadCast address. However I believe it is supported in Java, so perhaps someone on this forum would know how to access it using JavaObject.
 
Upvote 0
Top