public async Task StartListeningAsync( int port, ICommunicationInterface listenOn = null, bool allowMultipleBindToSamePort = false) { CheckCommunicationInterface(listenOn); var ipAddress = listenOn != null ? ((CommunicationInterface)listenOn).NativeIpAddress : IPAddress.Any; _tcpListener = new TcpListener(ipAddress, port) { ExclusiveAddressUse = !allowMultipleBindToSamePort }; try { _tcpListener.Start(); } catch (PlatformSocketException ex) { throw new PclSocketException(ex); } _tcpClientSubscribe = ObservableTcpSocketFromAsync.Subscribe( client => { _subjectTcpSocket.OnNext(client); }, ex => { _subjectTcpSocket.OnError(ex); }); }
#pragma warning disable 1998 public async Task StartListeningAsync( #pragma warning restore 1998 int port, ICommunicationInterface listenOn = null, bool allowMultipleBindToSamePort = false) { CheckCommunicationInterface(listenOn); var ipAddress = (listenOn as CommunicationsInterface)?.NativeIpAddress ?? IPAddress.Any; //_tcpListener = new TcpListener(ipAddress, port); try { _tcpListener = new TcpListener(ipAddress, port) { ExclusiveAddressUse = !allowMultipleBindToSamePort }; } catch (SocketException) { _tcpListener = new TcpListener(ipAddress, port); // Not all platforms need or accept the ExclusiveAddressUse option. Here we catch the exception if the platform does not need it. } try { _tcpListener.Start(); } catch (PlatformSocketException ex) { throw new PclSocketException(ex); } _tcpClientSubscribe = ObservableTcpSocketFromAsync.Subscribe( client => { _subjectTcpSocket.OnNext(client); }, ex => { Cleanup(); _subjectTcpSocket.OnError(ex); }, () => { Cleanup(); }); }