当前位置: 首页>>代码示例>>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;未经允许,请勿转载。