TcpChannel 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供一種使用 TCP 協定來傳送訊息的通道實作。
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
- 繼承
-
TcpChannel
- 實作
範例
以下程式碼範例說明如何使用 a TcpChannel 來設定遠端伺服器及其用戶端。 範例包含三個部分:伺服器、客戶端,以及伺服器與客戶端共用的遠端物件。
以下程式碼範例展示了一台伺服器:
#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();
}
}
以下程式碼範例展示了此伺服器的用戶端:
#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());
}
}
以下程式碼範例展示了伺服器與用戶端所使用的遠端物件:
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);
}
}
備註
Important
從這個類別使用不受信任的數據呼叫方法構成安全性風險。 僅使用信任的數據呼叫來自這個類別的方法。 如需詳細資訊,請參閱 驗證所有輸入。
通道跨越遠端邊界(例如應用程式域上的電腦間)傳送訊息。 這個 TcpChannel 類別是一個便利類別,結合了類別的功能 TcpClientChannel 與類別本身 TcpServerChannel 。
通道被 .NET 框架遠端基礎設施用於傳輸遠端通話。 當用戶端呼叫遠端物件時,該呼叫會序列化成由用戶端通道傳送並由伺服器通道接收的訊息。 接著被解序並處理。 任何回傳的值由伺服器通道傳送,並由用戶端通道接收。
若要執行額外的訊息處理,你可以指定 和 的實作,讓所有被 IClientChannelSinkProviderIServerChannelSinkProvider 處理 TcpChannel 的訊息都經過這些實作傳遞。
物件TcpChannel具有相關的組態屬性,可在執行時設定,無論是在設定檔中(透過呼叫靜態RemotingConfiguration.Configure方法)或程式化(將集合傳給IDictionaryTcpChannel建構器)中設定。 欲了解更多通道配置屬性,請參閱 通道與形式配置屬性。
建構函式
| 名稱 | Description |
|---|---|
| TcpChannel() |
初始化該類別的新實例 TcpChannel ,僅啟用用戶端通道,而非伺服器通道。 |
| TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
初始化一個具有指定配置屬性與匯入的類別新實例 TcpChannel 。 |
| TcpChannel(Int32) |
初始化一個新的類別實例 TcpChannel ,並以伺服器通道監聽指定的埠口。 |
屬性
| 名稱 | Description |
|---|---|
| ChannelData |
取得頻道特定的資料。 |
| ChannelName |
會得到目前頻道的名稱。 |
| ChannelPriority |
獲得當前頻道的優先權。 |
| IsSecured |
取得或設定一個布林值,指示目前通道是否安全。 |
方法
| 名稱 | Description |
|---|---|
| CreateMessageSink(String, Object, String) |
回傳一個通道訊息匯入器,將訊息傳送到指定的 URL 或通道資料物件。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| GetUrlsForUri(String) |
回傳一個包含指定 URI 物件的所有 URL 陣列,該物件託管於目前 TcpChannel的 。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Parse(String, String) |
從指定的 URL 中擷取通道 URI 及遠端知名物件 URI。 |
| StartListening(Object) |
指示當前頻道開始監聽請求。 |
| StopListening(Object) |
指示當前頻道停止監聽請求。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |