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
- 实现
示例
下面的代码示例演示如何使用 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);
}
}
注解
重要
使用不受信任的数据调用此类中的方法存在安全风险。 仅使用受信任的数据调用此类中的方法。 有关详细信息,请参阅 验证所有输入。
通道跨远程处理边界传输消息 (例如,在应用程序域) 上的计算机之间传输消息。 类TcpChannel是一个方便的类,它结合了 类和 TcpServerChannel 类的功能TcpClientChannel。
.NET Framework 远程处理基础结构使用通道来传输远程调用。 当客户端调用远程对象时,该调用将序列化为由客户端通道发送并由服务器通道接收的消息。 然后对其进行反序列化和处理。 任何返回的值都由服务器通道传输并由客户端通道接收。
若要对消息执行其他处理,可以指定 和 IServerChannelSinkProvider 的IClientChannelSinkProvider实现,通过这些实现传递由 TcpChannel 处理的所有消息。
TcpChannel对象具有关联的配置属性,可以在运行时通过调用静态RemotingConfiguration.Configure方法) (配置文件中设置这些属性,或通过将集合传递给IDictionaryTcpChannel构造函数) 以编程方式 (。 有关通道配置属性的详细信息,请参阅 通道和格式化程序配置属性。
构造函数
TcpChannel() |
初始化 TcpChannel 类的新实例,仅激活客户端信道,不激活服务器信道。 |
TcpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
使用指定的配置属性和接收器初始化 TcpChannel 类的新实例。 |
TcpChannel(Int32) |
使用侦听指定端口的服务器信道初始化 TcpChannel 类的新实例。 |
属性
ChannelData |
获取信道特定数据。 |
ChannelName |
获取当前信道的名称。 |
ChannelPriority |
获取当前信道的优先级。 |
IsSecured |
获取或设置一个布尔值,该值指示当前信道是否安全。 |
方法
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) |