TcpChannel Klasa
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Zapewnia implementację kanału, która używa protokołu TCP do przesyłania komunikatów.
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
- Dziedziczenie
-
TcpChannel
- Implementuje
Przykłady
W poniższym przykładzie kodu pokazano, jak używać elementu TcpChannel do konfigurowania serwera komunikacji zdalnie i jego klienta. Przykład zawiera trzy części, serwer, klienta i obiekt zdalny używany przez serwer i klienta.
Poniższy przykład kodu przedstawia serwer:
#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();
}
}
Poniższy przykład kodu przedstawia klienta dla tego serwera:
#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());
}
}
Poniższy przykład kodu przedstawia obiekt zdalny używany przez serwer i klienta:
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);
}
}
Uwagi
Ważne
Wywoływanie metod z tej klasy z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Wywołaj metody z tej klasy tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Weryfikowanie wszystkich danych wejściowych.
Kanały transportują komunikaty przez granice komunikacji zdalnie (na przykład między komputerami w domenach aplikacji). Klasa TcpChannel jest klasą wygodną łączącą funkcjonalność TcpClientChannel klasy i TcpServerChannel klasy.
Kanały są używane przez infrastrukturę komunikacji zdalnej .NET Framework do transportu połączeń zdalnych. Gdy klient wykonuje wywołanie obiektu zdalnego, wywołanie jest serializowane do komunikatu wysyłanego przez kanał klienta i odbierane przez kanał serwera. Następnie jest deserializowany i przetwarzany. Wszystkie zwrócone wartości są przesyłane przez kanał serwera i odbierane przez kanał klienta.
Aby wykonać dodatkowe przetwarzanie komunikatów, można określić implementacje IClientChannelSinkProvider elementów i IServerChannelSinkProvider , za pomocą których przekazywane są wszystkie komunikaty przetwarzane przez TcpChannel element .
Obiekt TcpChannel ma skojarzone właściwości konfiguracji, które można ustawić w czasie wykonywania w pliku konfiguracji (wywołując metodę statyczną RemotingConfiguration.Configure ) lub programowo (przekazując IDictionary kolekcję do konstruktora TcpChannel ). Aby uzyskać więcej informacji na temat właściwości konfiguracji kanału, zobacz Właściwości konfiguracji kanału i formatnika.
Konstruktory
TcpChannel() |
Inicjuje TcpChannel nowe wystąpienie klasy, aktywując tylko kanał klienta, a nie kanał serwera. |
TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
Inicjuje TcpChannel nowe wystąpienie klasy z określonymi właściwościami konfiguracji i ujściami. |
TcpChannel(Int32) |
Inicjuje TcpChannel nowe wystąpienie klasy przy użyciu kanału serwera, który nasłuchuje na określonym porcie. |
Właściwości
ChannelData |
Pobiera dane specyficzne dla kanału. |
ChannelName |
Pobiera nazwę bieżącego kanału. |
ChannelPriority |
Pobiera priorytet bieżącego kanału. |
IsSecured |
Pobiera lub ustawia wartość logiczną wskazującą, czy bieżący kanał jest bezpieczny. |
Metody
CreateMessageSink(String, Object, String) |
Zwraca ujście komunikatów kanału, który dostarcza komunikaty do określonego adresu URL lub obiektu danych kanału. |
Equals(Object) |
Określa, czy dany obiekt jest taki sam, jak bieżący obiekt. (Odziedziczone po Object) |
GetHashCode() |
Służy jako domyślna funkcja skrótu. (Odziedziczone po Object) |
GetType() |
Type Pobiera bieżące wystąpienie. (Odziedziczone po Object) |
GetUrlsForUri(String) |
Zwraca tablicę wszystkich adresów URL obiektu z określonym identyfikatorem URI hostowanym w bieżącym obiekcie TcpChannel. |
MemberwiseClone() |
Tworzy płytkią kopię bieżącego Objectelementu . (Odziedziczone po Object) |
Parse(String, String) |
Wyodrębnia identyfikator URI kanału i zdalny dobrze znany identyfikator URI obiektu z określonego adresu URL. |
StartListening(Object) |
Instruuje bieżący kanał, aby rozpocząć nasłuchiwanie żądań. |
StopListening(Object) |
Instruuje bieżący kanał, aby przestał nasłuchiwać żądań. |
ToString() |
Zwraca ciąg reprezentujący bieżący obiekt. (Odziedziczone po Object) |
Dotyczy
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla