TcpChannel Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Poskytuje implementaci kanálu, která používá protokol TCP k přenosu zpráv.
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
- Dědičnost
-
TcpChannel
- Implementuje
Příklady
Následující příklad kódu ukazuje, jak použít TcpChannel k nastavení serveru vzdálené komunikace a jeho klienta. Příklad obsahuje tři části: server, klient a vzdálený objekt používaný serverem a klientem.
Následující příklad kódu ukazuje 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();
}
}
Následující příklad kódu ukazuje klienta pro tento 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());
}
}
Následující příklad kódu ukazuje vzdálený objekt používaný serverem a klientem:
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);
}
}
Poznámky
Důležité
Volání metod z této třídy s nedůvěryhodnými daty představuje bezpečnostní riziko. Metody z této třídy volejte pouze s důvěryhodnými daty. Další informace najdete v tématu Ověření všech vstupů.
Kanály přenášejí zprávy přes hranice vzdálené komunikace (například mezi počítači v aplikačních doménách). Třída TcpChannel je třída pohodlí kombinující funkce TcpClientChannel třídy a TcpServerChannel třídy.
Kanály se používají v infrastruktuře vzdálené komunikace rozhraní .NET Framework k přenosu vzdálených volání. Když klient provede volání vzdáleného objektu, volání se serializuje do zprávy, která je odeslána klientským kanálem a přijata kanálem serveru. Pak se deserializuje a zpracuje. Všechny vrácené hodnoty jsou přenášeny kanálem serveru a přijaty klientským kanálem.
Chcete-li provádět další zpracování zpráv, můžete zadat implementace aIServerChannelSinkProvider, jejichž prostřednictvím jsou předány IClientChannelSinkProvider všechny zprávy zpracované nástrojemTcpChannel.
Objekt TcpChannel má přidružené vlastnosti konfigurace, které lze nastavit za běhu buď v konfiguračním souboru (vyvoláním statické RemotingConfiguration.Configure metody), nebo programově (předáním IDictionary kolekce konstruktoru TcpChannel ). Další informace o vlastnostech konfigurace kanálu najdete v tématu Vlastnosti konfigurace kanálu a formátovače.
Konstruktory
TcpChannel() |
Inicializuje novou instanci TcpChannel třídy, aktivuje pouze kanál klienta, a ne kanál serveru. |
TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
Inicializuje novou instanci TcpChannel třídy se zadanými vlastnostmi konfigurace a jímky. |
TcpChannel(Int32) |
Inicializuje novou instanci TcpChannel třídy se serverovým kanálem, který naslouchá na zadaném portu. |
Vlastnosti
ChannelData |
Získá data specifická pro kanál. |
ChannelName |
Získá název aktuálního kanálu. |
ChannelPriority |
Získá prioritu aktuálního kanálu. |
IsSecured |
Získá nebo nastaví logickou hodnotu, která označuje, zda je aktuální kanál zabezpečený. |
Metody
CreateMessageSink(String, Object, String) |
Vrátí jímku zpráv kanálu, která doručuje zprávy do zadané adresy URL nebo datového objektu kanálu. |
Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu. (Zděděno od Object) |
GetHashCode() |
Slouží jako výchozí hashovací funkce. (Zděděno od Object) |
GetType() |
Získá aktuální Type instanci. (Zděděno od Object) |
GetUrlsForUri(String) |
Vrátí pole všech adres URL objektu se zadaným identifikátorem URI hostovaným v aktuálním TcpChannelobjektu . |
MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Objectsouboru . (Zděděno od Object) |
Parse(String, String) |
Extrahuje identifikátor URI kanálu a vzdálený dobře známý identifikátor URI objektu ze zadané adresy URL. |
StartListening(Object) |
Dá aktuálnímu kanálu pokyn, aby začal naslouchat žádostem. |
StopListening(Object) |
Dá aktuálnímu kanálu pokyn, aby přestal naslouchat žádostem. |
ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Platí pro
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro