TcpChannel Classe

Définition

Fournit une implémentation de canal qui utilise le protocole TCP pour transmettre des messages.

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
Héritage
TcpChannel
Implémente

Exemples

L’exemple de code suivant montre comment utiliser un TcpChannel serveur de communication à distance et son client. L’exemple contient trois parties, un serveur, un client et un objet distant utilisé par le serveur et le client.

L’exemple de code suivant montre un serveur :

#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();
    }
}

L’exemple de code suivant montre un client pour ce serveur :

#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());
    }
}

L’exemple de code suivant montre l’objet distant utilisé par le serveur et le 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);
    }
}

Remarques

Important

L’appel de méthodes de cette classe avec des données non approuvées est un risque de sécurité. Appelez les méthodes de cette classe uniquement avec des données approuvées. Pour plus d’informations, consultez Valider toutes les entrées.

Les canaux transportent les messages entre les limites de communication à distance (par exemple, entre les ordinateurs sur les domaines d’application). La TcpChannel classe est une classe pratique combinant les fonctionnalités de la TcpClientChannel classe et de la TcpServerChannel classe.

Les canaux sont utilisés par l’infrastructure de communication à distance .NET Framework pour transporter les appels distants. Lorsqu’un client effectue un appel à un objet distant, l’appel est sérialisé dans un message envoyé par un canal client et reçu par un canal serveur. Il est ensuite désérialisé et traité. Toutes les valeurs retournées sont transmises par le canal serveur et reçues par le canal client.

Pour effectuer un traitement supplémentaire des messages, vous pouvez spécifier des implémentations du IClientChannelSinkProvider message et IServerChannelSinkProvider à travers lequel tous les messages traités par le TcpChannel message sont passés.

Un TcpChannel objet a associé des propriétés de configuration qui peuvent être définies au moment de l’exécution dans un fichier de configuration (en appelant la méthode statique RemotingConfiguration.Configure ) ou par programmation (en passant une IDictionary collection au TcpChannel constructeur).

Constructeurs

Nom Description
TcpChannel()

Initialise une nouvelle instance de la TcpChannel classe, activant uniquement un canal client et non un canal serveur.

TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)

Initialise une nouvelle instance de la TcpChannel classe avec les propriétés et récepteurs de configuration spécifiés.

TcpChannel(Int32)

Initialise une nouvelle instance de la TcpChannel classe avec un canal de serveur qui écoute sur le port spécifié.

Propriétés

Nom Description
ChannelData

Obtient les données spécifiques au canal.

ChannelName

Obtient le nom du canal actuel.

ChannelPriority

Obtient la priorité du canal actuel.

IsSecured

Obtient ou définit une valeur booléenne qui indique si le canal actuel est sécurisé.

Méthodes

Nom Description
CreateMessageSink(String, Object, String)

Retourne un récepteur de messages de canal qui remet des messages à l’URL ou à l’objet de données de canal spécifié.

Equals(Object)

Détermine si l'objet spécifié est identique à l'objet actuel.

(Hérité de Object)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient la Type de l’instance actuelle.

(Hérité de Object)
GetUrlsForUri(String)

Retourne un tableau de toutes les URL d’un objet avec l’URI spécifié, hébergé sur le fichier actif TcpChannel.

MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
Parse(String, String)

Extrait l’URI du canal et l’URI d’objet connu à distance à partir de l’URL spécifiée.

StartListening(Object)

Indique au canal actuel de commencer à écouter les demandes.

StopListening(Object)

Indique au canal actuel d’arrêter l’écoute des requêtes.

ToString()

Retourne une chaîne qui représente l’objet actuel.

(Hérité de Object)

S’applique à