private void PopulateAdapterList() { _localHostItems.Clear(); AdapterList.ItemsSource = _localHostItems; AdapterList.DisplayMemberPath = "DisplayString"; foreach (var localHostInfo in NetworkInformation.GetHostNames()) { if (localHostInfo.IPInformation == null) { continue; } var adapterItem = new LocalHostItem(localHostInfo); _localHostItems.Add(adapterItem); } }
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"); // サーバソケットの作成 var listener = new Windows.Networking.Sockets.DatagramSocket(); // メッセージを受信したときのハンドラ listener.MessageReceived += MessageReceivedAsync; if (!string.IsNullOrWhiteSpace(InboundBufferSize.Text)) { if (!uint.TryParse(InboundBufferSize.Text, out var inboundBufferSize)) { _rootPage.NotifyUser( "Please provide a positive numeric Inbound buffer size.", NotifyType.ErrorMessage); return; } try { // 受信時のデータバッファサイズをセット listener.Control.InboundBufferSizeInBytes = inboundBufferSize; } catch (ArgumentException) { _rootPage.NotifyUser("Please provide a valid Inbound buffer size.", NotifyType.ErrorMessage); return; } } // Address or Adapter binding 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; } CoreApplication.Properties.Add("serverAddress", selectedLocalHost.LocalHost.CanonicalName); } // 作成したソケットをアプリに保存する CoreApplication.Properties.Add("listener", listener); // Listenの開始 try { if (BindToAny.IsChecked == true) { // コントロールに入力されたポートでListenする await listener.BindServiceNameAsync(ServiceNameForListener.Text); _rootPage.NotifyUser("Listening", NotifyType.StatusMessage); } else if (BindToAddress.IsChecked == true) { if (selectedLocalHost == null) { return; } await listener.BindEndpointAsync(selectedLocalHost.LocalHost, ServiceNameForListener.Text); _rootPage.NotifyUser( "Listening on addrress " + selectedLocalHost.LocalHost.CanonicalName, NotifyType.StatusMessage); } else if (BindToAdapter.IsChecked == true) { if (selectedLocalHost == null) { return; } var 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("listenner"); if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } _rootPage.NotifyUser( "Start listening failed with error: " + exception.Message, NotifyType.ErrorMessage); } }