Export (0) Print
Expand All

SocketPermission Class

Controls rights to make or accept connections on a transport address.

Namespace: System.Net
Assembly: System (in system.dll)

[SerializableAttribute] 
public ref class SocketPermission sealed : public CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
public final class SocketPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
public final class SocketPermission extends CodeAccessPermission implements IUnrestrictedPermission

SocketPermission instances control permission to accept connections or initiate Socket connections. A Socket permission can be established for a host name or IP address, a port number, and a transport protocol.

NoteNote

Avoid creating socket permissions using host names, as these names have to be resolved to IP addresses, and this might block the stack.

The following example demonstrates how to use the SocketPermission class to set, change, and enforce various socket access restrictions.

// Creates a SocketPermission restricting access to and from all URIs.
SocketPermission^ mySocketPermission1 = gcnew SocketPermission( PermissionState::None );

// The socket to which this permission will apply will allow connections from www.contoso.com.
mySocketPermission1->AddPermission( NetworkAccess::Accept, TransportType::Tcp,  "www.contoso.com", 11000 );

// Creates a SocketPermission which will allow the target Socket to connect with www.southridgevideo.com.
SocketPermission^ mySocketPermission2 = gcnew SocketPermission( NetworkAccess::Connect,TransportType::Tcp, "www.southridgevideo.com",11002 );

// Creates a SocketPermission from the union of two SocketPermissions.
SocketPermission^ mySocketPermissionUnion =
   (SocketPermission^)( mySocketPermission1->Union( mySocketPermission2 ) );

// Checks to see if the union was successfully created by using the IsSubsetOf method.
if ( mySocketPermission1->IsSubsetOf( mySocketPermissionUnion ) &&
   mySocketPermission2->IsSubsetOf( mySocketPermissionUnion ) )
{
   Console::WriteLine(  "This union contains permissions from both mySocketPermission1 and mySocketPermission2" );
   
   // Prints the allowable accept URIs to the console.
   Console::WriteLine(  "This union accepts connections on :" );

   IEnumerator^ myEnumerator = mySocketPermissionUnion->AcceptList;
   while ( myEnumerator->MoveNext() )
   {
      Console::WriteLine( safe_cast<EndpointPermission^>( myEnumerator->Current )->ToString() );
   }
   
   // Prints the allowable connect URIs to the console.
   Console::WriteLine(  "This union permits connections to :" );

   myEnumerator = mySocketPermissionUnion->ConnectList;
   while ( myEnumerator->MoveNext() )
   {
      Console::WriteLine( safe_cast<EndpointPermission^>( myEnumerator->Current )->ToString() );
   }
}

// Creates a SocketPermission from the intersect of two SocketPermissions.
SocketPermission^ mySocketPermissionIntersect =
   (SocketPermission^)( mySocketPermission1->Intersect( mySocketPermissionUnion ) );

// mySocketPermissionIntersect should now contain the permissions of mySocketPermission1.
if ( mySocketPermission1->IsSubsetOf( mySocketPermissionIntersect ) )
{
   Console::WriteLine(  "This is expected" );
}

// mySocketPermissionIntersect should not contain the permissios of mySocketPermission2.
if ( mySocketPermission2->IsSubsetOf( mySocketPermissionIntersect ) )
{
   Console::WriteLine(  "This should not print" );
}

// Creates a copy of the intersect SocketPermission.
SocketPermission^ mySocketPermissionIntersectCopy =
   (SocketPermission^)( mySocketPermissionIntersect->Copy() );
if ( mySocketPermissionIntersectCopy->Equals( mySocketPermissionIntersect ) )
{
   Console::WriteLine(  "Copy successfull" );
}

// Converts a SocketPermission to XML format and then immediately converts it back to a SocketPermission.
mySocketPermission1->FromXml( mySocketPermission1->ToXml() );

// Checks to see if permission for this socket resource is unrestricted.  If it is, then there is no need to
// demand that permissions be enforced.
if ( mySocketPermissionUnion->IsUnrestricted() )
{
   //Do nothing.  There are no restrictions.
}
else
{
   // Enforces the permissions found in mySocketPermissionUnion on any Socket Resources used below this statement. 
   mySocketPermissionUnion->Demand();
}

IPHostEntry^ myIpHostEntry = Dns::Resolve(  "www.contoso.com" );
IPEndPoint^ myLocalEndPoint = gcnew IPEndPoint( myIpHostEntry->AddressList[ 0 ], 11000 );

Socket^ s = gcnew Socket( myLocalEndPoint->Address->AddressFamily,
   SocketType::Stream,
   ProtocolType::Tcp );
try
{
   s->Connect( myLocalEndPoint );
}
catch ( Exception^ e ) 
{
   Console::Write(  "Exception Thrown: " );
   Console::WriteLine( e->ToString() );
}

// Perform all socket operations in here.
s->Close();

// Creates a SocketPermission restricting access to and from all URIs.
SocketPermission mySocketPermission1 
    = new SocketPermission(PermissionState.None);
// The socket to which this permission will apply will allow 
// connections from www.contoso.com.
mySocketPermission1.AddPermission(NetworkAccess.Accept, 
    TransportType.Tcp, "www.contoso.com", 11000);
// Creates a SocketPermission which will allow the target Socket to 
// connect with www.southridgevideo.com.
SocketPermission mySocketPermission2 
    = new SocketPermission(NetworkAccess.Connect, 
    TransportType.Tcp, "www.southridgevideo.com", 11002);
// Creates a SocketPermission from the union of two SocketPermissions.
SocketPermission mySocketPermissionUnion 
    = (SocketPermission)mySocketPermission1.Union(mySocketPermission2);
// Checks to see if the union was successfully created by using the 
// IsSubsetOf method.
if (mySocketPermission1.IsSubsetOf(mySocketPermissionUnion) 
    && mySocketPermission2.IsSubsetOf(mySocketPermissionUnion)) {
    Console.WriteLine("This union contains permissions from both "                                 + "mySocketPermission1 and mySocketPermission2");
    // Prints the allowable accept URIs to the console.
    Console.WriteLine("This union accepts connections on :");

    IEnumerator myEnumerator = mySocketPermissionUnion.
        get_AcceptList();
    while (myEnumerator.MoveNext()) {
        Console.WriteLine(((EndpointPermission)myEnumerator.
            get_Current()).ToString());
    }
    // Prints the allowable connect URIs to the console.
    Console.WriteLine("This union permits connections to :");

    myEnumerator = mySocketPermissionUnion.get_ConnectList();
    while (myEnumerator.MoveNext()) {
        Console.WriteLine(((EndpointPermission)myEnumerator.
            get_Current()).ToString());
    }
}

// Creates a SocketPermission from the intersect of two 
// SocketPermissions.
SocketPermission mySocketPermissionIntersect 
    = (SocketPermission)mySocketPermission1.
    Intersect(mySocketPermissionUnion);
// mySocketPermissionIntersect should now contain the permissions of                       // mySocketPermission1.
if (mySocketPermission1.IsSubsetOf(mySocketPermissionIntersect)) {
    Console.WriteLine("This is expected");
}
// mySocketPermissionIntersect should not contain the permissios of                        // mySocketPermission2.
if (mySocketPermission2.IsSubsetOf(mySocketPermissionIntersect)) {
    Console.WriteLine("This should not print");
}

// Creates a copy of the intersect SocketPermission.
SocketPermission mySocketPermissionIntersectCopy 
    = (SocketPermission)mySocketPermissionIntersect.Copy();

if (mySocketPermissionIntersectCopy.
    Equals(mySocketPermissionIntersect)) {
    Console.WriteLine("Copy successfull");
}

// Converts a SocketPermission to XML format and then immediately 
// converts it back to a SocketPermission.
mySocketPermission1.FromXml(mySocketPermission1.ToXml());
// Checks to see if permission for this socket resource is 
// unrestricted.  If it is, then there is no need to
// demand that permissions be enforced.
if (mySocketPermissionUnion.IsUnrestricted()) {
    //Do nothing.  There are no restrictions.
}
else {
    // Enforces the permissions found in mySocketPermissionUnion on 
    // any Socket Resources used below this statement. 
    mySocketPermissionUnion.Demand();
}

IPHostEntry myIpHostEntry = Dns.Resolve("www.contoso.com");
IPEndPoint myLocalEndPoint = new IPEndPoint(myIpHostEntry.
    get_AddressList()[0], 11000);

Socket s = new Socket(myLocalEndPoint.get_Address().
    get_AddressFamily(), SocketType.Stream, ProtocolType.Tcp);
try {
    s.Connect(myLocalEndPoint);
}
catch (System.Exception e) {
    Console.WriteLine("Exception Thrown: " + e.ToString());
}
// Perform all socket operations in here.
s.Close();

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft