當前位置: 首頁>>代碼示例>>C#>>正文


C# StreamSocketListener.BindServiceNameAsync方法代碼示例

本文整理匯總了C#中Windows.Networking.Sockets.StreamSocketListener.BindServiceNameAsync方法的典型用法代碼示例。如果您正苦於以下問題:C# StreamSocketListener.BindServiceNameAsync方法的具體用法?C# StreamSocketListener.BindServiceNameAsync怎麽用?C# StreamSocketListener.BindServiceNameAsync使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Windows.Networking.Sockets.StreamSocketListener的用法示例。


在下文中一共展示了StreamSocketListener.BindServiceNameAsync方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: StartListeningAsync

        /// <summary>
        ///     Binds the <code>TcpSocketListener</code> to the specified port on all endpoints and listens for TCP connections.
        /// </summary>
        /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param>
        /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public Task StartListeningAsync(int port, ICommsInterface listenOn = null)
        {
            if (listenOn != null && !listenOn.IsUsable)
                throw new InvalidOperationException("Cannot listen on an unusable interface. Check the IsUsable property before attemping to bind.");
            
            _listenCanceller = new CancellationTokenSource();
            _backingStreamSocketListener = new StreamSocketListener();

            _backingStreamSocketListener.ConnectionReceived += (sender, args) =>
            {
                var nativeSocket = args.Socket;
                var wrappedSocket = new TcpSocketClient(nativeSocket, _bufferSize);

                var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedSocket);
                if (ConnectionReceived != null)
                    ConnectionReceived(this, eventArgs);
            };

            var sn = port == 0 ? "" : port.ToString();
#if !WP80    
            if (listenOn != null)
            {
                var adapter = ((CommsInterface)listenOn).NativeNetworkAdapter;

                return _backingStreamSocketListener
                            .BindServiceNameAsync(sn, SocketProtectionLevel.PlainSocket, adapter)
                            .AsTask();
            }
            else
#endif
                return _backingStreamSocketListener
                            .BindServiceNameAsync(sn)
                            .AsTask();
        }
開發者ID:2B2i,項目名稱:sockets-for-pcl,代碼行數:40,代碼來源:TcpSocketListener.cs

示例2: Start

 /// <summary>
 /// 受付開始
 /// </summary>
 public async void Start()
 {
     listener = new StreamSocketListener();
     listener.ConnectionReceived += Listener_ConnectionReceived;
     // await listener.BindEndpointAsync(LOCALHOST, PORT.ToString());
     await listener.BindServiceNameAsync(PORT.ToString());
 }
開發者ID:moonmile,項目名稱:winiot-samples,代碼行數:10,代碼來源:SimpleWebServer.cs

示例3: StartListen

        /// <summary>
        /// Server listens to specified port and accepts connection from client
        /// </summary>
        public async void StartListen()
        {
            var ip = (networkInterface != null)
                ? GetInterfaceIpAddress()
                : IPAddress.Any;

            tcpServer = new StreamSocketListener();
            await tcpServer.BindServiceNameAsync(portNumber.ToString());
            tcpServer.ConnectionReceived += TcpServer_ConnectionReceived;
            
            isRunning = true;
            // Keep accepting client connection
            while (isRunning)
            {
                if (!tcpServer.Pending())
                {
                    Thread.Sleep(500);
                    continue;
                }
                // New client is connected, call event to handle it
                var clientThread = new Thread(NewClient);
                var tcpClient = tcpServer.AcceptTcpClient();
                tcpClient.ReceiveTimeout = 20000;
                clientThread.Start(tcpClient.Client);
            }
        }
開發者ID:notdef1ned,項目名稱:voice-chat-winrt,代碼行數:29,代碼來源:ChatServer.cs

示例4: listenButton_Click

        /// <summary>
        /// 服務器監聽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  async void listenButton_Click(object sender, RoutedEventArgs e)
        {
            if (listener != null)
            {
                await new MessageDialog("監聽已經啟動了").ShowAsync();
                return;
            }
            listener= new StreamSocketListener();
            //監聽後連接的事件OnConnection
            listener.ConnectionReceived += OnConnection;
            //開始監聽操作
            try
            {
                await listener.BindServiceNameAsync("22112");
                await new MessageDialog("正在監聽").ShowAsync();
            }

            catch (Exception ex)
            {
                listener = null;
                //未知異常
                if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }
            }
        }
開發者ID:745322878,項目名稱:Code,代碼行數:32,代碼來源:MainPage.xaml.cs

示例5: StartServer_Click_1

        private async void StartServer_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                if (listener == null)
                {
                    // We call it 'local', becuase if this connection doesn't succeed, we do not want
                    // to loose the possible previous conneted listener.
                    listener = new StreamSocketListener();

                    // ConnectionReceived handler must be set before BindServiceNameAsync is called, if not
                    // "A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)"
                    // error occurs.
                    listener.ConnectionReceived += OnConnectionReceived;

                    // Trying to bind more than once to the same port throws "Only one usage of each socket
                    // address (protocol/network address/port) is normally permitted. (Exception from
                    // HRESULT: 0x80072740)" exception.
                    await listener.BindServiceNameAsync("80");
                    DisplayOutput(TcpServerOutput, "Listening.");
                }
            }
            catch (Exception ex)
            {
                DisplayOutput(TcpServerOutput, ex.ToString());
            }
        }
開發者ID:kiewic,項目名稱:Projects,代碼行數:27,代碼來源:MainPage.xaml.cs

示例6: Start

        public async void Start()
        {
            this.listener = new StreamSocketListener();
            this.listener.ConnectionReceived += Listener_ConnectionReceived;

            await listener.BindServiceNameAsync(this.port.ToString());
        }
開發者ID:philipp2500,項目名稱:Remote-Content-Show,代碼行數:7,代碼來源:Server.cs

示例7: Star

        public async void Star()
        {
            try
            {
                //Fecha a conexão com a porta que está escutando atualmente
                if (listener != null)
                {
                    await listener.CancelIOAsync();
                    listener.Dispose();
                    listener = null;
                }

                //Criar uma nova instancia do listerner
                listener = new StreamSocketListener();

                //Adiciona o evento de conexão recebida ao método Listener_ConnectionReceived
                listener.ConnectionReceived += Listener_ConnectionReceived;
                //Espera fazer o bind da porta
                await listener.BindServiceNameAsync(Port.ToString());
            }
            catch (Exception e)
            {
                //Caso aconteça um erro, dispara o evento de erro
                if (OnError != null)
                    OnError(e.Message);
            }
        }
開發者ID:lillo42,項目名稱:IoT,代碼行數:27,代碼來源:SocketServer.cs

示例8: InitializeRfcommServer

        /// <summary>
        /// Initialize a server socket listening for incoming Bluetooth Rfcomm connections
        /// </summary>
        async void InitializeRfcommServer()
        {
            try
            {
                ListenButton.IsEnabled = false;
                DisconnectButton.IsEnabled = true;

                rfcommProvider = await RfcommServiceProvider.CreateAsync(
                    RfcommServiceId.FromUuid(RfcommChatServiceUuid));

                // Create a listener for this service and start listening
                socketListener = new StreamSocketListener();
                socketListener.ConnectionReceived += OnConnectionReceived;

                await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(),
                    SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

                // Set the SDP attributes and start Bluetooth advertising
                InitializeServiceSdpAttributes(rfcommProvider);
                rfcommProvider.StartAdvertising(socketListener);

                NotifyStatus("Listening for incoming connections");
            }
            catch (Exception e)
            {
                NotifyError(e);
            }
        }
開發者ID:ckc,項目名稱:WinApp,代碼行數:31,代碼來源:Scenario2_ChatServer.xaml.cs

示例9: Start

 public async void Start() {
     if (isServerActive) return;
     isServerActive = true;
     listener = new StreamSocketListener();
     listener.Control.QualityOfService = SocketQualityOfService.Normal;
     listener.ConnectionReceived += ListenerConnectionReceived;
     await listener.BindServiceNameAsync(listeningPort.ToString());
     WriteIpAddress();
 }
開發者ID:nikita-afonasov,項目名稱:WinPhone8Driver,代碼行數:9,代碼來源:Server.cs

示例10: StartTcpServiceAsync

        private async Task<StreamSocketListener> StartTcpServiceAsync()
        {
            var tcpService = new StreamSocketListener();

            tcpService.ConnectionReceived += TcpService_ConnectionReceived;
            await tcpService.BindServiceNameAsync("50000");
            

            return tcpService;
        }
開發者ID:PRIESt512,項目名稱:Beowulf,代碼行數:10,代碼來源:MainPage.xaml.cs

示例11: Start

        /// <include file='../../../_Doc/System.xml' path='doc/members/member[@name="M:System.Net.Sockets.TcpListener.Start"]/*' />
        public void Start()
        {
            _client = null;

            _listener = new StreamSocketListener();
            _listener.ConnectionReceived += OnConnectionReceived;

            // Binding to specific endpoint is currently not supported.
            _event.Reset();
            Task.Run(async () => await _listener.BindServiceNameAsync(_port));
        }
開發者ID:BeeINSIGHT,項目名稱:shim,代碼行數:12,代碼來源:TcpListener.Store.cs

示例12: StartServer

 public virtual async Task StartServer()
 {
     try
     {
         _listener = CreateStreamSocketListener();
         await _listener.BindServiceNameAsync(Port.ToString());
     }
     catch (Exception ex)
     {
             
         throw;
     }
 }
開發者ID:rherlt,項目名稱:SimpleUniversalWebserver,代碼行數:13,代碼來源:HttpServer.cs

示例13: StartListing_Click

        private async void StartListing_Click(object sender, RoutedEventArgs e)
        {
            // Listener für Annahme von TCP-Verbindungsanfragen erzeugen
            _listener = new StreamSocketListener();

            // Eventhandler registrieren, um Verbindungsanfragen zu bearbeiten
            _listener.ConnectionReceived += Listener_ConnectionReceived;

            // Lauschen und warten auf Verbindungen starten
            Status.Text = "Starte Listener...";
            await _listener.BindServiceNameAsync(Port);
            Status.Text = "Listener bereit.";
        }
開發者ID:GregOnNet,項目名稱:WP8BookSamples,代碼行數:13,代碼來源:ServerPage.xaml.cs

示例14: StartListen

        /// <summary>
        /// Server listens to specified port and accepts connection from client
        /// </summary>
        public async void StartListen()
        {
           // var ip = (networkInterface != null)
           //     ? GetInterfaceIpAddress()
           //     : IPAddress.Any;

            tcpServer = new StreamSocketListener();
            tcpServer.ConnectionReceived += TcpServer_ConnectionReceived;
            await tcpServer.BindServiceNameAsync(portNumber.ToString(),SocketProtectionLevel.PlainSocket);

            
            isRunning = true;
            
        }
開發者ID:notdef1ned,項目名稱:voice-chat-winrt,代碼行數:17,代碼來源:ChatServer.cs

示例15: startListener

        private async Task<StreamSocketListener> startListener()
        {
            // Create the listening socket
            StreamSocketListener listener = new StreamSocketListener();

            // This is the event that gets triggered when somebody connects to us
            listener.ConnectionReceived += listener_ConnectionReceived;

            // We'll bind to the port given in portTextbox
            await listener.BindServiceNameAsync(this.portTextbox.Text);

            // Return our socket
            return listener;
        }
開發者ID:EE590-Spring2014,項目名稱:Materials,代碼行數:14,代碼來源:MainPage.xaml.cs


注:本文中的Windows.Networking.Sockets.StreamSocketListener.BindServiceNameAsync方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。