TcpChannel Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Tillhandahåller en kanalimplementering som använder TCP-protokollet för att överföra meddelanden.
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
- Arv
-
TcpChannel
- Implementeringar
Exempel
Följande kodexempel visar hur du använder en TcpChannel för att konfigurera en fjärrkommunikationsserver och dess klient. Exemplet innehåller tre delar, en server, en klient och ett fjärrobjekt som används av servern och klienten.
Följande kodexempel visar en 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();
}
}
I följande kodexempel visas en klient för den här servern:
#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());
}
}
I följande kodexempel visas fjärrobjektet som används av servern och klienten:
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);
}
}
Kommentarer
Important
Att anropa metoder från den här klassen med ej betrodda data är en säkerhetsrisk. Anropa metoderna från den här klassen endast med betrodda data. För mer information, se Verifiera alla indata.
Kanaler transporterar meddelanden över fjärrkommunikationsgränser (till exempel mellan datorer i programdomäner). Klassen TcpChannel är en bekvämlighetsklass som kombinerar funktionerna i TcpClientChannel klassen och TcpServerChannel klassen.
Kanaler används av .NET Framework-fjärrkommunikationsinfrastrukturen för att transportera fjärranrop. När en klient anropar ett fjärrobjekt serialiseras anropet till ett meddelande som skickas av en klientkanal och tas emot av en serverkanal. Den deserialiseras och bearbetas sedan. Alla returnerade värden överförs av serverkanalen och tas emot av klientkanalen.
Om du vill utföra ytterligare bearbetning av meddelanden kan du ange implementeringar av IClientChannelSinkProvider och IServerChannelSinkProvider genom vilka alla meddelanden som bearbetas av TcpChannel skickas.
Ett TcpChannel objekt har associerade konfigurationsegenskaper som kan anges vid körning antingen i en konfigurationsfil (genom att anropa den statiska RemotingConfiguration.Configure metoden) eller programmatiskt (genom att skicka en IDictionary samling till TcpChannel konstruktorn). Mer information om egenskaper för kanalkonfiguration finns i Konfigurationsegenskaper för kanal och formatering.
Konstruktorer
| Name | Description |
|---|---|
| TcpChannel() |
Initierar en ny instans av TcpChannel klassen, aktiverar endast en klientkanal och inte en serverkanal. |
| TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
Initierar en ny instans av TcpChannel klassen med de angivna konfigurationsegenskaperna och mottagare. |
| TcpChannel(Int32) |
Initierar en ny instans av TcpChannel klassen med en serverkanal som lyssnar på den angivna porten. |
Egenskaper
| Name | Description |
|---|---|
| ChannelData |
Hämtar kanalspecifika data. |
| ChannelName |
Hämtar namnet på den aktuella kanalen. |
| ChannelPriority |
Hämtar prioriteten för den aktuella kanalen. |
| IsSecured |
Hämtar eller anger ett booleskt värde som anger om den aktuella kanalen är säker. |
Metoder
| Name | Description |
|---|---|
| CreateMessageSink(String, Object, String) |
Returnerar en kanalmeddelandemottagare som levererar meddelanden till den angivna URL:en eller kanaldataobjektet. |
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetType() |
Hämtar den aktuella instansen Type . (Ärvd från Object) |
| GetUrlsForUri(String) |
Returnerar en matris med alla URL:er för ett objekt med angiven URI som finns på den aktuella TcpChannel. |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| Parse(String, String) |
Extraherar kanal-URI:n och den välkända fjärrobjekt-URI:n från den angivna URL:en. |
| StartListening(Object) |
Instruerar den aktuella kanalen att börja lyssna efter begäranden. |
| StopListening(Object) |
Instruerar den aktuella kanalen att sluta lyssna efter begäranden. |
| ToString() |
Returnerar en sträng som representerar det aktuella objektet. (Ärvd från Object) |