From 27415553a55ce35a03f96840807fab632449a31a Mon Sep 17 00:00:00 2001 From: Daniele Perrella <32111035+dvgniele@users.noreply.github.com> Date: Thu, 4 Oct 2018 21:22:51 +0200 Subject: [PATCH] C# UDP Client UDP Clienton C# UWP app --- Others/C# UDP Client | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Others/C# UDP Client diff --git a/Others/C# UDP Client b/Others/C# UDP Client new file mode 100644 index 0000000..b8f35f0 --- /dev/null +++ b/Others/C# UDP Client @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Text; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI; +using Windows.UI.Popups; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + + +namespace UWPTestClientUDP +{ + public sealed partial class MainPage : Page + { + public MainPage() + { + this.InitializeComponent(); + } + + private void btn_Click(object sender, RoutedEventArgs e) + { + + errorblk.Text = ""; + + MessageDialog dialog = new MessageDialog(""); + + try + { + if (AddressBox.Text == "") + dialog.Content = "Insert IP Address"; + + if(AddressBox.Text != "") + { + Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + + var addressText = AddressBox.Text.Replace(',', '.'); + AddressBox.Text = addressText; + + IPAddress broadcast = IPAddress.Parse(addressText); + + byte[] sendbuf = Encoding.ASCII.GetBytes(txtblk.Text); + IPEndPoint ep = new IPEndPoint(broadcast, 11000); + + s.SendTo(sendbuf, ep); + + errorblk.Foreground = new SolidColorBrush(Colors.Green); + errorblk.Text = "Message Sent"; + } + } + catch (Exception ex) + { + errorblk.Foreground = new SolidColorBrush(Colors.Red); + + errorblk.Text = ex.ToString(); + } + } + } +}