TcpChannel Classe

Definizione

Fornisce un'implementazione del canale che utilizza il protocollo TCP per la trasmissione dei messaggi.

public ref class TcpChannel : System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::IChannelSender
public ref class TcpChannel : System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::IChannelSender, System::Runtime::Remoting::Channels::ISecurableChannel
public class TcpChannel : System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.IChannelSender
public class TcpChannel : System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.IChannelSender, System.Runtime.Remoting.Channels.ISecurableChannel
type TcpChannel = class
    interface IChannelReceiver
    interface IChannelSender
    interface IChannel
type TcpChannel = class
    interface IChannelReceiver
    interface IChannelSender
    interface IChannel
    interface ISecurableChannel
type TcpChannel = class
    interface IChannelReceiver
    interface IChannel
    interface IChannelSender
    interface ISecurableChannel
Public Class TcpChannel
Implements IChannelReceiver, IChannelSender
Public Class TcpChannel
Implements IChannelReceiver, IChannelSender, ISecurableChannel
Ereditarietà
TcpChannel
Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato come usare un TcpChannel oggetto per configurare un server di comunicazione remota e il relativo client. L'esempio contiene tre parti, un server, un client e un oggetto remoto usato dal server e dal client.

Nell'esempio di codice seguente viene illustrato un server:

#using <System.Runtime.Remoting.dll>
#using <System.dll>
#using <common.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
using namespace System::Security::Permissions;

[SecurityPermission(SecurityAction::Demand)]
int main(array<String^>^ args)
{
    // Create the server channel.
    TcpChannel^ serverChannel = gcnew TcpChannel(9090);

    // Register the server channel.
    ChannelServices::RegisterChannel(serverChannel);

    // Show the name of the channel.
    Console::WriteLine("The name of the channel is {0}.", 
        serverChannel->ChannelName);

    // Show the priority of the channel.
    Console::WriteLine("The priority of the channel is {0}.", 
        serverChannel->ChannelPriority);

    // Show the URIs associated with the channel.
    ChannelDataStore^ data = (ChannelDataStore^) serverChannel->ChannelData;
    for each (String^ uri in data->ChannelUris)
    {
        Console::WriteLine("The channel URI is {0}.", uri);
    }

    // Expose an object for remote calls.
    RemotingConfiguration::RegisterWellKnownServiceType(
        RemoteObject::typeid, "RemoteObject.rem", 
        WellKnownObjectMode::Singleton);

    // Parse the channel's URI.
    array<String^>^ urls = serverChannel->GetUrlsForUri("RemoteObject.rem");
    if (urls->Length > 0)
    {
        String^ objectUrl = urls[0];
        String^ objectUri;
        String^ channelUri = serverChannel->Parse(objectUrl, objectUri);
        Console::WriteLine("The object URL is {0}.", objectUrl);
        Console::WriteLine("The object URI is {0}.", objectUri);
        Console::WriteLine("The channel URI is {0}.", channelUri);
    }
        
    // Wait for the user prompt.
    Console::WriteLine("Press ENTER to exit the server.");
    Console::ReadLine();
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class Server
{
    public static void Main(string[] args)
    {
        // Create the server channel.
        TcpChannel serverChannel = new TcpChannel(9090);

        // Register the server channel.
        ChannelServices.RegisterChannel(serverChannel);

        // Show the name of the channel.
        Console.WriteLine("The name of the channel is {0}.",
            serverChannel.ChannelName);

        // Show the priority of the channel.
        Console.WriteLine("The priority of the channel is {0}.",
            serverChannel.ChannelPriority);

        // Show the URIs associated with the channel.
        ChannelDataStore data = (ChannelDataStore) serverChannel.ChannelData;
        foreach (string uri in data.ChannelUris)
        {
            Console.WriteLine("The channel URI is {0}.", uri);
        }

        // Expose an object for remote calls.
        RemotingConfiguration.RegisterWellKnownServiceType(
            typeof(RemoteObject), "RemoteObject.rem",
            WellKnownObjectMode.Singleton);

        // Parse the channel's URI.
        string[] urls = serverChannel.GetUrlsForUri("RemoteObject.rem");
        if (urls.Length > 0)
        {
            string objectUrl = urls[0];
            string objectUri;
            string channelUri = serverChannel.Parse(objectUrl, out objectUri);
            Console.WriteLine("The object URL is {0}.", objectUrl);
            Console.WriteLine("The object URI is {0}.", objectUri);
            Console.WriteLine("The channel URI is {0}.", channelUri);
        }

        // Wait for the user prompt.
        Console.WriteLine("Press ENTER to exit the server.");
        Console.ReadLine();
    }
}

Nell'esempio di codice seguente viene illustrato un client per questo server:

#using <System.Runtime.Remoting.dll>
#using <System.dll>
#using <common.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
using namespace System::Security::Permissions;

int main(array<String^>^ args)
{
    // Create the channel.
    TcpChannel^ clientChannel = gcnew TcpChannel();

    // Register the channel.
    ChannelServices::RegisterChannel(clientChannel, false);

    // Register as client for remote object.
    WellKnownClientTypeEntry^ remoteType = gcnew WellKnownClientTypeEntry(
        RemoteObject::typeid,"tcp://localhost:9090/RemoteObject.rem");
    RemotingConfiguration::RegisterWellKnownClientType(remoteType);

    // Create a message sink.
    String^ objectUri;
    System::Runtime::Remoting::Messaging::IMessageSink^ messageSink = 
        clientChannel->CreateMessageSink(
            "tcp://localhost:9090/RemoteObject.rem", nullptr,
            objectUri);
    Console::WriteLine("The URI of the message sink is {0}.", 
        objectUri);
    if (messageSink != nullptr)
    {
        Console::WriteLine("The type of the message sink is {0}.", 
            messageSink->GetType()->ToString());
    }

    // Create an instance of the remote object.
    RemoteObject^ service = gcnew RemoteObject(); 

    // Invoke a method on the remote object.
    Console::WriteLine("The client is invoking the remote object.");
    Console::WriteLine("The remote object has been called {0} times.",
        service->GetCount());
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class Client
{
    public static void Main(string[] args)
    {
        // Create the channel.
        TcpChannel clientChannel = new TcpChannel();

        // Register the channel.
        ChannelServices.RegisterChannel(clientChannel, false);

        // Register as client for remote object.
        WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(
            typeof(RemoteObject),"tcp://localhost:9090/RemoteObject.rem");
        RemotingConfiguration.RegisterWellKnownClientType(remoteType);

        // Create a message sink.
        string objectUri;
        System.Runtime.Remoting.Messaging.IMessageSink messageSink =
            clientChannel.CreateMessageSink(
                "tcp://localhost:9090/RemoteObject.rem", null,
                out objectUri);
        Console.WriteLine("The URI of the message sink is {0}.",
            objectUri);
        if (messageSink != null)
        {
            Console.WriteLine("The type of the message sink is {0}.",
                messageSink.GetType().ToString());
        }

        // Create an instance of the remote object.
        RemoteObject service = new RemoteObject();

        // Invoke a method on the remote object.
        Console.WriteLine("The client is invoking the remote object.");
        Console.WriteLine("The remote object has been called {0} times.",
            service.GetCount());
    }
}

Nell'esempio di codice seguente viene illustrato l'oggetto remoto usato dal server e dal client:

using namespace System;
using namespace System::Runtime::Remoting;

// Remote object.
public ref class RemoteObject: public MarshalByRefObject
{
private:
   int callCount;

public:
   RemoteObject()
      : callCount( 0 )
   {}

   int GetCount()
   {
      callCount++;
      return (callCount);
   }

};
using System;
using System.Runtime.Remoting;

// Remote object.
public class RemoteObject : MarshalByRefObject
{
    private int callCount = 0;

    public int GetCount()
    {
        callCount++;
        return(callCount);
    }
}

Commenti

Importante

La chiamata a metodi da questa classe con dati non attendibili costituisce un rischio per la sicurezza. Chiamare i metodi da questa classe solo con dati attendibili. Per altre informazioni, vedere Convalidare tutti gli input.

I canali trasportano i messaggi tra limiti remoti, ad esempio tra computer nei domini dell'applicazione. La TcpChannel classe è una classe di praticità che combina la funzionalità della TcpClientChannel classe e della TcpServerChannel classe.

I canali vengono usati dall'infrastruttura remota di .NET Framework per il trasporto di chiamate remote. Quando un client effettua una chiamata a un oggetto remoto, la chiamata viene serializzata in un messaggio inviato da un canale client e ricevuto da un canale server. Viene quindi deserializzato ed elaborato. Tutti i valori restituiti vengono trasmessi dal canale del server e ricevuti dal canale client.

Per eseguire un'elaborazione aggiuntiva dei messaggi, è possibile specificare le implementazioni dell'oggetto IClientChannelSinkProvider e IServerChannelSinkProvider tramite cui vengono passati tutti i messaggi elaborati dall'oggetto TcpChannel .

Un TcpChannel oggetto ha associato proprietà di configurazione che possono essere impostate in fase di esecuzione in un file di configurazione (richiamando il metodo statico RemotingConfiguration.Configure ) o a livello di codice (passando una IDictionary raccolta al TcpChannel costruttore). Per altre informazioni sulle proprietà di configurazione del canale, vedere Proprietà di configurazione canale e formattatore.

Costruttori

TcpChannel()

Inizializza una nuova istanza della classe TcpChannel attivando solo un canale del client e non un canale del server.

TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)

Inizializza una nuova istanza della classe TcpChannel con le proprietà di configurazione e i sink specificati.

TcpChannel(Int32)

Inizializza una nuova istanza della classe TcpChannel con un canale server in attesa sulla porta specificata.

Proprietà

ChannelData

Ottiene i dati specifici del canale.

ChannelName

Ottiene il nome del canale corrente.

ChannelPriority

Ottiene la priorità del canale corrente.

IsSecured

Ottiene o imposta un valore booleano che indica se il canale corrente è protetto.

Metodi

CreateMessageSink(String, Object, String)

Restituisce un sink dei messaggi del canale che invia messaggi all'URL o all'oggetto dati del canale specificato.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
GetUrlsForUri(String)

Restituisce una matrice di tutti gli URL di un oggetto con l'URI specificato, ospitato nell'oggetto TcpChannel corrente.

MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
Parse(String, String)

Estrae dall'URL specificato l'URI del canale e quello dell'oggetto remoto conosciuto.

StartListening(Object)

Indica al canale corrente di mettersi in attesa di richieste.

StopListening(Object)

Indica al canale corrente di interrompere l'attesa di richieste.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a