Exemple #1
0
        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);
                }
            }
        }
Exemple #2
0
        private async void StartListener_Click(object sender, RoutedEventArgs e)
        {
            // Overriding the listener here is safe as it will be deleted once all references to it are gone.
            // However, in many cases this is a dangerous pattern to override data semi-randomly (each time user
            // clicked the button) so we block it here.
            if (CoreApplication.Properties.ContainsKey("listener"))
            {
                rootPage.NotifyUser(
                    "This step has already been executed. Please move to the next one.",
                    NotifyType.ErrorMessage);
                return;
            }

            if (String.IsNullOrEmpty(ServiceNameForListener.Text))
            {
                rootPage.NotifyUser("Please provide a service name.", NotifyType.ErrorMessage);
                return;
            }

            CoreApplication.Properties.Remove("serverAddress");
            CoreApplication.Properties.Remove("adapter");

            LocalHostItem selectedLocalHost = null;

            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);

            StreamSocketListener listener = new StreamSocketListener();

            listener.ConnectionReceived += OnConnection;

            // If necessary, tweak the listener's control options before carrying out the bind operation.
            // These options will be automatically applied to the connected StreamSockets resulting from
            // incoming connections (i.e., those passed as arguments to the ConnectionReceived event handler).
            // Refer to the StreamSocketListenerControl class' MSDN documentation for the full list of control options.
            listener.Control.KeepAlive = false;

            // Save the socket, so subsequent steps can use it.
            CoreApplication.Properties.Add("listener", listener);

            // Start listen operation.
            try
            {
                // Try to bind to a specific address.
                await listener.BindEndpointAsync(selectedLocalHost.LocalHost, ServiceNameForListener.Text);

                rootPage.NotifyUser(
                    "Listening on address " + selectedLocalHost.LocalHost.CanonicalName,
                    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);
            }
        }