本文整理汇总了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();
}
示例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());
}
示例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);
}
}
示例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;
}
}
}
示例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());
}
}
示例6: Start
public async void Start()
{
this.listener = new StreamSocketListener();
this.listener.ConnectionReceived += Listener_ConnectionReceived;
await listener.BindServiceNameAsync(this.port.ToString());
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例10: StartTcpServiceAsync
private async Task<StreamSocketListener> StartTcpServiceAsync()
{
var tcpService = new StreamSocketListener();
tcpService.ConnectionReceived += TcpService_ConnectionReceived;
await tcpService.BindServiceNameAsync("50000");
return tcpService;
}
示例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));
}
示例12: StartServer
public virtual async Task StartServer()
{
try
{
_listener = CreateStreamSocketListener();
await _listener.BindServiceNameAsync(Port.ToString());
}
catch (Exception ex)
{
throw;
}
}
示例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.";
}
示例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;
}
示例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;
}