Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / ManagedLibraries / Remoting / Channels / CORE / ExclusiveTcpListener.cs / 1305376 / ExclusiveTcpListener.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- using System; using System.Net; using System.Net.Sockets; namespace System.Runtime.Remoting.Channels { // This class provides a TcpListener that is capable of setting the ExclusiveAddressUse flag // on a socket, which will prevent another app from hijacking our port. This flag is not supported // on Win9x, so we just omit the call to SetSocketOption on non-NT platforms. internal class ExclusiveTcpListener : TcpListener { internal ExclusiveTcpListener(IPAddress localaddr, int port) : base(localaddr, port) {} // Start will attempt to start listening. If exclusiveAddressUse is true, then // we will attempt to use the ExclusiveAddressUse flag, but if bind fails (which will // happen for a regular user on win2k and xp), we try again without the flag. internal void Start(bool exclusiveAddressUse) { // we only attempt to set the socket option if // 1. the exclusiveAddressUse param is true // 2. the platform is NT - this option is unavailable on other platforms // 3. Server is not null - if it IS null, base.Start will throw a nice error for us // 4. the listener is not already listening - it's too late in that case (base.Start will return immediately) bool attemptSetSocketOption = exclusiveAddressUse && #if !FEATURE_PAL Environment.OSVersion.Platform == PlatformID.Win32NT && #endif // !FEATURE_PAL base.Server != null && !base.Active; if (attemptSetSocketOption) { // Attempt to set the option. We won't actually find out if this fails until // we try to bind (which happens in base.Start()). base.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, 1); } try { base.Start(); } catch (SocketException) { if (attemptSetSocketOption) { // Turn off the option and try again - maybe this process doesn't have // permission to use the option. Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, 0); base.Start(); } else { // It wasn't because we set the ExclusiveAddressUse option - let the // exception bubble up throw; } } } internal bool IsListening { get { return Active; } } } } // namespace System.Runtime.Remoting.Channels // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- using System; using System.Net; using System.Net.Sockets; namespace System.Runtime.Remoting.Channels { // This class provides a TcpListener that is capable of setting the ExclusiveAddressUse flag // on a socket, which will prevent another app from hijacking our port. This flag is not supported // on Win9x, so we just omit the call to SetSocketOption on non-NT platforms. internal class ExclusiveTcpListener : TcpListener { internal ExclusiveTcpListener(IPAddress localaddr, int port) : base(localaddr, port) {} // Start will attempt to start listening. If exclusiveAddressUse is true, then // we will attempt to use the ExclusiveAddressUse flag, but if bind fails (which will // happen for a regular user on win2k and xp), we try again without the flag. internal void Start(bool exclusiveAddressUse) { // we only attempt to set the socket option if // 1. the exclusiveAddressUse param is true // 2. the platform is NT - this option is unavailable on other platforms // 3. Server is not null - if it IS null, base.Start will throw a nice error for us // 4. the listener is not already listening - it's too late in that case (base.Start will return immediately) bool attemptSetSocketOption = exclusiveAddressUse && #if !FEATURE_PAL Environment.OSVersion.Platform == PlatformID.Win32NT && #endif // !FEATURE_PAL base.Server != null && !base.Active; if (attemptSetSocketOption) { // Attempt to set the option. We won't actually find out if this fails until // we try to bind (which happens in base.Start()). base.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, 1); } try { base.Start(); } catch (SocketException) { if (attemptSetSocketOption) { // Turn off the option and try again - maybe this process doesn't have // permission to use the option. Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, 0); base.Start(); } else { // It wasn't because we set the ExclusiveAddressUse option - let the // exception bubble up throw; } } } internal bool IsListening { get { return Active; } } } } // namespace System.Runtime.Remoting.Channels // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- __Filters.cs
- PropertyChangedEventManager.cs
- ActivationArguments.cs
- PrimaryKeyTypeConverter.cs
- TableLayoutSettings.cs
- TypeDependencyAttribute.cs
- DataGridViewDesigner.cs
- ConnectivityStatus.cs
- PkcsMisc.cs
- LocationSectionRecord.cs
- EdgeModeValidation.cs
- CodeDirectoryCompiler.cs
- IPipelineRuntime.cs
- XslCompiledTransform.cs
- ServiceOperationWrapper.cs
- PerformanceCounterPermissionEntry.cs
- PresentationTraceSources.cs
- RbTree.cs
- PauseStoryboard.cs
- IMembershipProvider.cs
- SqlGatherProducedAliases.cs
- DefaultWorkflowLoaderService.cs
- IQueryable.cs
- RuntimeConfigLKG.cs
- XmlSchemaFacet.cs
- DashStyles.cs
- SafeFileMappingHandle.cs
- DurableInstance.cs
- TreeNodeBindingCollection.cs
- HtmlElement.cs
- PerformanceCountersElement.cs
- QueryableDataSource.cs
- PassportIdentity.cs
- DesignerDataRelationship.cs
- EnterpriseServicesHelper.cs
- FormClosingEvent.cs
- KeyNotFoundException.cs
- SoapReflector.cs
- FlagsAttribute.cs
- FunctionImportElement.cs
- CellParagraph.cs
- EntityContainerEmitter.cs
- TablePattern.cs
- BookmarkTable.cs
- DateRangeEvent.cs
- BaseValidatorDesigner.cs
- HostProtectionException.cs
- Random.cs
- Attributes.cs
- ImageSourceValueSerializer.cs
- BooleanExpr.cs
- GradientSpreadMethodValidation.cs
- DataGridHeaderBorder.cs
- CodeAccessSecurityEngine.cs
- PathSegmentCollection.cs
- BitmapEffect.cs
- BasicKeyConstraint.cs
- ConnectionString.cs
- WebPartDisplayModeCancelEventArgs.cs
- HuffCodec.cs
- FixedSOMImage.cs
- DBBindings.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- PrintPreviewControl.cs
- LiteralControl.cs
- CultureInfo.cs
- SQLDoubleStorage.cs
- RtfToXamlLexer.cs
- Pointer.cs
- Comparer.cs
- WeakReferenceList.cs
- InputProcessorProfiles.cs
- XamlSerializerUtil.cs
- ConnectionManagementSection.cs
- LingerOption.cs
- JoinTreeSlot.cs
- HighlightVisual.cs
- SafeViewOfFileHandle.cs
- SerializerProvider.cs
- FontStyles.cs
- ImportContext.cs
- EtwTrace.cs
- BitmapCache.cs
- Msec.cs
- DocumentViewerConstants.cs
- RSAPKCS1SignatureFormatter.cs
- DockingAttribute.cs
- ZipIOCentralDirectoryDigitalSignature.cs
- AsymmetricSignatureFormatter.cs
- WebServiceResponseDesigner.cs
- EntityParameter.cs
- SqlConnectionHelper.cs
- NegotiateStream.cs
- MasterPageBuildProvider.cs
- CompiledRegexRunner.cs
- StylusTip.cs
- HtmlInputRadioButton.cs
- DbConnectionPoolOptions.cs
- WhitespaceSignificantCollectionAttribute.cs
- DataBindEngine.cs