From 73562acb583528adc9052bbf6f2b136221c48329 Mon Sep 17 00:00:00 2001 From: raspopov Date: Sun, 28 Dec 2025 19:49:17 +0300 Subject: [PATCH] Allow broadcast requests to the servers It allows the client to use requests of the form: "coap://255.255.255.255/.well-known/core" to search for primitive servers on the local area network (LAN) that do not support multicasts. It also enables the client to reuse sockets, since the packets have a built-in mechanism for determining if the response matches the request. --- CoAP.NET/Channel/UDPChannel.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/CoAP.NET/Channel/UDPChannel.cs b/CoAP.NET/Channel/UDPChannel.cs index e4fdb03..9feae33 100644 --- a/CoAP.NET/Channel/UDPChannel.cs +++ b/CoAP.NET/Channel/UDPChannel.cs @@ -294,13 +294,24 @@ private UDPSocket SetupUDPSocket(AddressFamily addressFamily, Int32 bufferSize) { UDPSocket socket = NewUDPSocket(addressFamily, bufferSize); + try + { + socket.Socket.EnableBroadcast = true; + socket.Socket.ExclusiveAddressUse = false; + socket.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + } + catch (Exception) + { + // ignore + } + // do not throw SocketError.ConnectionReset by ignoring ICMP Port Unreachable const Int32 SIO_UDP_CONNRESET = -1744830452; try { socket.Socket.IOControl(SIO_UDP_CONNRESET, new Byte[] { 0 }, null); } - catch (Exception e) + catch (Exception) { // ignore }