Download

UDP Multicast- Network Interface Selection

Hi,
I’ve created a project to receive and send data from/ to an external application.

Sending works perfectly fine, receiving works (almost) fine BUT there is one catch: The network interface gets auto-selected.
The problem is that i can’t select the right (user specified) network interface to receive data from, which leads to the project not working on computers, which have multiple network interfaces (those seem to select the wrong interface).

If I disable all other networks (e.g. VirtualBox Host-Only Network and Npcap Loopback Adapter) on those PCs the correct interface is selected and receiving data works!

So how can I bind to a specific interface?
I’ve tried .BoundToEndpoint, .BoundToAddress and .BoundToPort (IP Address/ “ReceiveIP” is set via ui to the computers IP (192.168.1.XX)) in order to bind to a specified address, but that didn’t do the trick.
I’ve also tried variations like specifying only .BoundToEndpoint or only .BoundToAddress and .BoundToPort.
Also tried to listen to any address (FIPv4Address::Any).

Did I miss a method/ flag in FUdpSocketBuilder? (Searched for it, didn’t find any).

To see the address the socket is bound to, I’ve created “ReceiverAddress” to log the bound IP and Port.
Weird thing is, that the specified/ correct IP and port is displayed, but receiving data doesn’t work- still didn’t bound to the correct interface, although claiming it?

Code:



void UUDPComponent::StartReceiveSocketListening(const int32 InListenPort */*= 3002*/*)
  {
      ReceiverAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
        FIPv4Address Addr;
      FIPv4Address::Parse( ReceiveIP.IsEmpty() ? TEXT("0.0.0.0"): ReceiveIP, Addr);
        FIPv4Address Addr1;
      FIPv4Address::Parse(MulticastGroupIp.IsEmpty() ? TEXT("237.37.37.37") : MulticastGroupIp, Addr1);
        *//Create Socket*
      FIPv4Endpoint Endpoint(Addr, InListenPort);
      FIPv4Endpoint AnyEndpoint(FIPv4Address::Any, InListenPort);
        if (MulticastGroupIp.IsEmpty())
      {
          ReceiverSocket = FUdpSocketBuilder(*ReceiveSocketName)
              .AsNonBlocking()
              .AsReusable()
              .BoundToEndpoint(Endpoint)
              .WithReceiveBufferSize(BufferSize);
      }
      else
      {
          ReceiverSocket = FUdpSocketBuilder(*ReceiveSocketName)
              .WithMulticastLoopback()
              .WithMulticastTtl(MulticastTtl)
              .WithBroadcast()
              .JoinedToGroup(Addr1)
              .AsNonBlocking()
              .AsReusable()
              .BoundToEndpoint(Endpoint)
              .BoundToAddress(Addr)
              .BoundToPort(InListenPort)
              .WithReceiveBufferSize(BufferSize)
              .Build();
      }
        FTimespan ThreadWaitTime = FTimespan::FromMilliseconds(100);
      FString ThreadName = FString::Printf(TEXT("UDP RECEIVER-%s"), *UKismetSystemLibrary::GetDisplayName(this));
        if (ReceiverSocket && !MulticastGroupIp.IsEmpty())
      {
          ReceiverSocket->SetBroadcast(true);
          if (GEngine)
          {
              ReceiverSocket->GetAddress(*ReceiverAddress);
              FString StringAddress = ReceiverAddress->ToString(true);
              GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Bound to %s"), *StringAddress));
          }
      }
        UDPReceiver = new FUdpSocketReceiver(ReceiverSocket, ThreadWaitTime, *ThreadName);
        UDPReceiver->OnDataReceived().BindUObject(this, &UUDPComponent::OnDataReceivedDelegate);
      OnReceiveSocketStartedListening.Broadcast();
        UDPReceiver->Start();
  }
 



MulticastTimeToLive is set to 32, so that shouldn’t be an issue.
Receiver IP, Port, MulticastGroupIp can be specified via ui during runtime, if those values are cahnged the receiver socket is closed and opened again to the refresh the connection parameters.

Using Unreal Engine 4.21.2 on Windows 10.
Any help is appreciated!

Cheers