TcpServerChannel 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
實作一個伺服器通道用於遠端通話,使用 TCP 協定傳送訊息。
public ref class TcpServerChannel : System::Runtime::Remoting::Channels::IChannelReceiver
public ref class TcpServerChannel : System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::ISecurableChannel
public class TcpServerChannel : System.Runtime.Remoting.Channels.IChannelReceiver
public class TcpServerChannel : System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.ISecurableChannel
type TcpServerChannel = class
interface IChannelReceiver
interface IChannel
type TcpServerChannel = class
interface IChannelReceiver
interface IChannel
interface ISecurableChannel
Public Class TcpServerChannel
Implements IChannelReceiver
Public Class TcpServerChannel
Implements IChannelReceiver, ISecurableChannel
- 繼承
-
TcpServerChannel
- 實作
範例
以下程式碼範例展示了可可調存型態的使用。
using namespace System;
using namespace System::Runtime::Remoting;
public ref class Remotable: public MarshalByRefObject
{
private:
int callCount;
public:
Remotable()
: callCount( 0 )
{}
int GetCount()
{
callCount++;
return (callCount);
}
};
using System;
using System.Runtime.Remoting;
public class Remotable : MarshalByRefObject
{
private int callCount = 0;
public int GetCount()
{
callCount++;
return(callCount);
}
}
以下程式碼範例展示了如何使用該 TcpServerChannel 類別來暴露可可調存型別。
#using <System.Runtime.Remoting.dll>
#using <System.dll>
#using <Remotable.dll>
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
int main()
{
// Set up a server channel.
TcpServerChannel^ serverChannel = gcnew TcpServerChannel( 9090 );
ChannelServices::RegisterChannel( serverChannel );
// Expose an object for remote calls.
RemotingConfiguration::RegisterWellKnownServiceType( Remotable::typeid, "Remotable.rem", WellKnownObjectMode::Singleton );
// Show the name and priority of the channel.
Console::WriteLine( "Channel Name: {0}", serverChannel->ChannelName );
Console::WriteLine( "Channel Priority: {0}", serverChannel->ChannelPriority );
// Show the URIs associated with the channel.
ChannelDataStore^ data = dynamic_cast<ChannelDataStore^>(serverChannel->ChannelData);
System::Collections::IEnumerator^ myEnum = data->ChannelUris->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ uri = safe_cast<String^>(myEnum->Current);
Console::WriteLine( uri );
}
// Wait for method calls.
Console::WriteLine( "Listening..." );
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()
{
// Set up a server channel.
TcpServerChannel serverChannel = new TcpServerChannel(9090);
ChannelServices.RegisterChannel(serverChannel);
// Expose an object for remote calls.
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Remotable), "Remotable.rem", WellKnownObjectMode.Singleton
);
// Show the name and priority of the channel.
Console.WriteLine("Channel Name: {0}", serverChannel.ChannelName);
Console.WriteLine("Channel Priority: {0}", serverChannel.ChannelPriority);
// Show the URIs associated with the channel.
ChannelDataStore data = (ChannelDataStore) serverChannel.ChannelData;
foreach (string uri in data.ChannelUris)
{
Console.WriteLine(uri);
}
// Wait for method calls.
Console.WriteLine("Listening...");
Console.ReadLine();
}
}
備註
Important
從這個類別使用不受信任的數據呼叫方法構成安全性風險。 僅使用信任的數據呼叫來自這個類別的方法。 如需詳細資訊,請參閱 驗證所有輸入。
通道透過遠端邊界(例如電腦或應用程式域)傳輸訊息。 該 TcpServerChannel 類別透過 TCP 協定傳輸訊息。
通道被 .NET 框架遠端基礎設施用於傳輸遠端通話。 當用戶端呼叫遠端物件時,該呼叫會序列化成由用戶端通道傳送並由伺服器通道接收的訊息。 接著被解序並處理。 任何回傳的值由伺服器通道傳送,並由用戶端通道接收。
若要在伺服器端執行額外訊息處理,你可以指定一個介面實作 IServerChannelSinkProvider ,讓實例處理的所有訊息 TcpServerChannel 都經過此介面傳遞。
該 TcpServerChannel 實例接受以二進位或 SOAP 格式序列化的訊息。
物件TcpServerChannel具有相關的組態屬性,可在執行時設定,無論是在設定檔中(透過呼叫靜態RemotingConfiguration.Configure方法)或程式化(將集合傳給IDictionaryTcpServerChannel建構器)中設定。 關於這些配置屬性的列表,請參見 通道與材料配置屬性。
建構函式
| 名稱 | Description |
|---|---|
| TcpServerChannel(IDictionary, IServerChannelSinkProvider, IAuthorizeRemotingConnection) |
初始化一個帶有指定通道屬性、sink 與授權提供者的新類別實例 TcpServerChannel 。 |
| TcpServerChannel(IDictionary, IServerChannelSinkProvider) |
初始化一個具有指定通道屬性與 sink 的類別新實例 TcpServerChannel 。 |
| TcpServerChannel(Int32) |
初始化一個在指定埠口監聽的類別新實例 TcpServerChannel 。 |
| TcpServerChannel(String, Int32, IServerChannelSinkProvider) |
初始化一個以該名稱為基礎的類別新實例,該實例 TcpServerChannel 監聽指定的埠,並使用指定的匯入。 |
| TcpServerChannel(String, Int32) |
初始化一個以指定名稱並監聽指定埠的類別新實例 TcpServerChannel 。 |
屬性
| 名稱 | Description |
|---|---|
| ChannelData |
取得頻道特定的資料。 |
| ChannelName |
會得到目前頻道的名稱。 |
| ChannelPriority |
獲得當前頻道的優先權。 |
| IsSecured |
取得或設定一個布林值,指示目前通道是否安全。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetChannelUri() |
回傳當前頻道的 URI。 |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| GetUrlsForUri(String) |
回傳一個包含指定 URI 物件的所有 URL 陣列,該物件托管於目前 TcpChannel 實例上。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Parse(String, String) |
從指定的 URL 中擷取通道 URI 及遠端知名物件 URI。 |
| StartListening(Object) |
在呼叫停止接收該通道的方法後,指示當前通道開始監聽該通道 StopListening(Object) 。 |
| StopListening(Object) |
指示當前頻道停止監聽請求。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |