Home Forums Support Simple Broadcast and Receive of Raw Bytes using UDP Reply To: Simple Broadcast and Receive of Raw Bytes using UDP

#2709
Anonymous
Inactive

WPF App with 6 buttons will do the trick.

2 buttons to Broadcast UDP to either Port 10000 or 20000

Click=btnClick_TxTo10000
Click=btnClick_TxTo20000

and 4 buttons to create udp listener, stop listening and start listening on either 10000 or 20000

Click=btnClick_CreateUDPListener
Click=btnClick_StopListening
Click=btnClick_ListenUDPOn10000
Click=btnClick_ListenUDPOn20000

In this situation, I am just reusing the UDPListener and not recreating. Same exception in either case though.


<Window x:Class="TestNetworkComms.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="NetworkComms Test" Height="200" Width="712" Closing="Window_Closing">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="Tx 10000" Margin="3" Width="80" Background="Orange" Click="btnClick_TxTo10000"/>
            <Button Content="Tx 20000" Margin="3" Width="80" Background="Orange" Click="btnClick_TxTo20000"/>
            <Button Content="UDP Input" Margin="3" Width="80" Background="DarkGreen" Click="btnClick_CreateUDPListener"/>
            <Button Content="UDP Input" Margin="3" Width="80" Background="DarkRed" Click="btnClick_StopListening"/>
            <Button Content="UDP Debug 1" Margin="3" Width="80" Background="SteelBlue" Click="btnClick_ListenUDPOn10000"/>
            <Button Content="UDP Debug 2" Margin="3" Width="80" Background="SteelBlue" Click="btnClick_ListenUDPOn20000"/>
        </StackPanel>

    </Grid>
</Window>
    
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            optionsToUseForUDPOutput = new SendReceiveOptions<NullSerializer>();
            optionsToUseForUDPInput = new SendReceiveOptions<NullSerializer>();

            NetworkComms.EnableLogging();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NetworkComms.DisableLogging();
            NetworkComms.Shutdown();
        }

        #region UDP Output
        SendReceiveOptions optionsToUseForUDPOutput;
        private void btnClick_TxTo10000(object sender, RoutedEventArgs e)
        {
            byte[] byteDataToSend= new byte[] { 1, 2, 3, 4};

            using (Packet sendPacket = new Packet("Unmanaged", byteDataToSend, optionsToUseForUDPOutput))
            {
                UDPConnection.SendObject<byte[]>(sendPacket, new IPEndPoint(IPAddress.Broadcast, 10000), optionsToUseForUDPOutput, ApplicationLayerProtocolStatus.Disabled );
            }
        }

        private void btnClick_TxTo20000(object sender, RoutedEventArgs e)
        {
            byte[] byteDataToSend= new byte[] { 11, 12, 13, 14};

            using (Packet sendPacket = new Packet("Unmanaged", byteDataToSend, optionsToUseForUDPOutput))
            {
                UDPConnection.SendObject<byte[]>(sendPacket, new IPEndPoint(IPAddress.Broadcast, 20000), optionsToUseForUDPOutput, ApplicationLayerProtocolStatus.Disabled);
            }
        }
        #endregion

        #region UDP Input
        SendReceiveOptions optionsToUseForUDPInput;
        UDPConnectionListener udpListener= null;
        private void btnClick_CreateUDPListener(object sender, RoutedEventArgs e)
        {
            udpListener = new UDPConnectionListener(optionsToUseForUDPInput, ApplicationLayerProtocolStatus.Disabled, UDPOptions.None);

            //Add a packet handler for dealing with incoming unmanaged data
            udpListener.AppendIncomingUnmanagedPacketHandler(HandleIncomingUDPPacket);

        }

        private void btnClick_StopListening(object sender, RoutedEventArgs e)
        {
            if ( udpListener.IsListening )
                Connection.StopListening(udpListener);
        }

        private void btnClick_ListenUDPOn10000(object sender, RoutedEventArgs e)
        {
            Connection.StartListening(udpListener, new IPEndPoint(IPAddress.Any, 10000));
        }

        private void btnClick_ListenUDPOn20000(object sender, RoutedEventArgs e)
        {
            Connection.StartListening(udpListener, new IPEndPoint(IPAddress.Any, 20000));
        }

        private void HandleIncomingUDPPacket(PacketHeader header, Connection connection, byte[] array)
        {
            string sz = string.Format("Received {0} bytes from ", array.Length);
            System.Diagnostics.Trace.WriteLine(sz + connection.ToString());
        }

        #endregion
    }

Cheers