/// <summary> /// Populates the NetworkAdapter list /// </summary> private void PopulateAdapterList() { localHostItems.Clear(); AdapterList.ItemsSource = localHostItems; AdapterList.DisplayMemberPath = "DisplayString"; foreach (HostName localHostInfo in NetworkInformation.GetHostNames()) { if (localHostInfo.IPInformation != null) { LocalHostItem adapterItem = new LocalHostItem(localHostInfo); localHostItems.Add(adapterItem); } } }
/// <summary> /// This is the click handler for the 'StartListener' button. /// </summary> /// <param name="sender">Object for which the event was generated.</param> /// <param name="e">Event's parameters.</param> private async void StartListener_Click(object sender, RoutedEventArgs e) { if (String.IsNullOrEmpty(ServiceNameForListener.Text)) { rootPage.NotifyUser("Please provide a service name.", NotifyType.ErrorMessage); return; } if (CoreApplication.Properties.ContainsKey("listener")) { rootPage.NotifyUser( "This step has already been executed. Please move to the next one.", NotifyType.ErrorMessage); return; } CoreApplication.Properties.Remove("serverAddress"); DatagramSocket listener = new DatagramSocket(); listener.MessageReceived += MessageReceived; if (!String.IsNullOrWhiteSpace(InboundBufferSize.Text)) { uint inboundBufferSize = 0; if (!uint.TryParse(InboundBufferSize.Text, out inboundBufferSize)) { rootPage.NotifyUser( "Please provide a positive numeric Inbound buffer size.", NotifyType.ErrorMessage); return; } try { // Running both client and server on localhost will most likely not reach the buffer limit. listener.Control.InboundBufferSizeInBytes = inboundBufferSize; } catch (ArgumentException) { rootPage.NotifyUser("Please provide a valid Inbound buffer size.", NotifyType.ErrorMessage); return; } } LocalHostItem selectedLocalHost = null; if ((BindToAddress.IsChecked == true) || (BindToAdapter.IsChecked == true)) { selectedLocalHost = (LocalHostItem)AdapterList.SelectedItem; if (selectedLocalHost == null) { rootPage.NotifyUser("Please select an address / adapter.", NotifyType.ErrorMessage); return; } // The user selected an address. For demo purposes, we ensure that connect will be using the same // address. CoreApplication.Properties.Add("serverAddress", selectedLocalHost.LocalHost.CanonicalName); } // Save the socket, so subsequent steps can use it. CoreApplication.Properties.Add("listener", listener); // Start listen operation. try { if (BindToAny.IsChecked == true) { // Don't limit traffic to an address or an adapter. await listener.BindServiceNameAsync(ServiceNameForListener.Text); rootPage.NotifyUser("Listening", NotifyType.StatusMessage); } else if (BindToAddress.IsChecked == true) { // Try to bind to a specific address. await listener.BindEndpointAsync(selectedLocalHost.LocalHost, ServiceNameForListener.Text); rootPage.NotifyUser( "Listening on address " + selectedLocalHost.LocalHost.CanonicalName, NotifyType.StatusMessage); } else if (BindToAdapter.IsChecked == true) { // Try to limit traffic to the selected adapter. // This option will be overriden by interfaces with weak-host or forwarding modes enabled. NetworkAdapter selectedAdapter = selectedLocalHost.LocalHost.IPInformation.NetworkAdapter; await listener.BindServiceNameAsync(ServiceNameForListener.Text, selectedAdapter); rootPage.NotifyUser( "Listening on adapter " + selectedAdapter.NetworkAdapterId, NotifyType.StatusMessage); } } catch (Exception exception) { CoreApplication.Properties.Remove("listener"); // If this is an unknown status it means that the error is fatal and retry will likely fail. if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } rootPage.NotifyUser( "Start listening failed with error: " + exception.Message, NotifyType.ErrorMessage); } }