dart:io, Flutter, UDP Socket (RawDatagramSocket) socket.listen not receiving packages, when powershell client does

Viewed 23

Context

I want to be able to have 2 flutter clients send each other data over a UDP Socket using a RawDatagramSocket in Dart using dart:io. both clients know each other IP-Address and port they are listening and receiving on

The current problem:

When sending a UDP packet to a flutter client hosted on the same LAN as the sender the UDP packet does not get received by the socket.listen() method.

When sending a UDP packet from either a powershell window, or flutter client to an UDP powershell listerner the packet does get received and can be read.

Question: How is it possible for the powershell client to receive the packet but the flutter client doesnt receive it
while sharing both the same IP-Address and port listening on?

Flutter Client

When sending a packet to this client listening on PORT 1338, the listen doesn't print anything indicating it doesnt receive the packet.

class ChatCubit extends Cubit<ChatState> {
  ChatCubit() : super(ChatLoading());

  String addressValue = 'IP OF THE TARGET MACHINE';

  late RawDatagramSocket udpSocket;

  Future main() async {
    udpSocket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 1338);
    udpSocket.broadcastEnabled = true;

    print('############# Listening to Messages #############');

    udpSocket.listen((event) {
      print("recv e: $event");
      if (event == RawSocketEvent.read) {
        print(utf8.decode(udpSocket.receive()!.data));
      }
    }, onError: (error) {
      print("error: $error");
    }, onDone: () {
      print("done!");
    }, cancelOnError: true);

    print('${udpSocket.address.address}:${udpSocket.port}');
  }

  void sendMessage(String basicMessage) async {
    print('############# Sending Message #############');

    udpSocket.send(const Utf8Codec().encode(basicMessage), InternetAddress(addressValue), 1337);
  }
}

Sender powershell

Using this powershell script to send an UDP packet to the flutter client results in the client not receiving anything

function Send-UdpDatagram
{
      Param ([string] $EndPoint, 
      [int] $Port, 
      [string] $Message)

      $IP = [System.Net.Dns]::GetHostAddresses($EndPoint) 
      $Address = [System.Net.IPAddress]::Parse($IP) 
      $EndPoints = New-Object System.Net.IPEndPoint($Address, $Port) 
      $Socket = New-Object System.Net.Sockets.UDPClient 
      $EncodedText = [Text.Encoding]::ASCII.GetBytes($Message) 
      $SendMessage = $Socket.Send($EncodedText, $EncodedText.Length, $EndPoints) 
      $Socket.Close() 
} 

powershell command: Send-UdpDatagram -EndPoint "IP OF TARGET MACHINE" -Port 1338 -Message "test.mymetric:0|c"

Receiver powershell

Using this powershell script to listen for UDP packets that will arrive on port 1338, it SUCCESFULLY receives the packet, using either a different flutter client that uses the same code but uses sendMessage to port 1338 and listens on 1337. Or the sender powershell script.

param( $address="Any", $port=1338)


try{
    $endpoint = new-object System.Net.IPEndPoint( [IPAddress]::$address, $port )
    $udpclient = new-object System.Net.Sockets.UdpClient $port
}
catch{
    throw $_
    exit -1
}

Write-Host "Press ESC to stop the udp server ..." -fore yellow
Write-Host ""
while( $true )
{
    if( $host.ui.RawUi.KeyAvailable )
    {
        $key = $host.ui.RawUI.ReadKey( "NoEcho,IncludeKeyUp,IncludeKeyDown" )
        if( $key.VirtualKeyCode -eq 27 )
        {    break    }
    }

    if( $udpclient.Available )
    {
        $content = $udpclient.Receive( [ref]$endpoint )
        Write-Host "$($endpoint.Address.IPAddressToString):$($endpoint.Port) $([Text.Encoding]::ASCII.GetString($content))"
    }
}

$udpclient.Close( )

The big question

How is it possible that sending from a flutter client or powershell window to a powershell window listener works. But sending a UDP packet to a flutter client and try to listen for it it simply doesnt want to receive it.

0 Answers
Related