如何去注册多个TCP/HTTP通道 ?

zhongasen 2009-09-12 09:07:26
我在学习Remoting 的时,我想把一个程序做成即是服务器又是客户端,
那么就去注册两个TCP ,一个用了监听客户端,一个用于和另一个服务器端进行通信
程序大致如下:
TcpChannel tcpService = new TcpChannel(8089);
ChannelServices.RegisterChannel(tcpService );
RemotingConfiguration.RegisterWellKnownServiceType(typeof(showMessage.showmsg), "tcpChart", WellKnownObjectMode.Singleton);
/*******************/
TcpChannel tcpClient = new TcpChannel();
ChannelServices.RegisterChannel(tcpClient ); showMessage.showmsg msg = (showMessage.showmsg ) Activator.GetObject(typeof(showMessage.showmsg),"tcp://localhost:8088/tcpChart");
while(true)
{
string msgstr = Console.ReadLine();
msg.show(msgstr);
}
可是当再去注册第二个TCP时出错: TCP通道已注册。。。。 
请高手帮我解释一下,是程序问题呢? 还是其它的原因? 为什么不能注册两个TCP通道
...全文
224 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
作为客户端的程序,不需要注册通道。如果分开写,可能就清楚了。你写在一起,所以把自己搞混了。
zhongasen 2009-09-12
  • 打赏
  • 举报
回复
另外还可以注册多个服务:
RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "Visitor", WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "Admin", WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "Console", WellKnownObjectMode.Singleton);
zhongasen 2009-09-12
  • 打赏
  • 举报
回复

我知道呀,我没有注册多个端口哟!? 不知道为什么?

另外:我加上其它的端口也没有用?


刚试过上面的程序其实可以不用再去注册通道了! 就那一个通道就可以实现监听和发送的功能! 
如下:
server1:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
//using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;

namespace remoting_p2p
{
class Program
{
static void Main(string[] args)
{
string msg=string.Empty;
TcpChannel tcp = new TcpChannel(8099);
ChannelServices.RegisterChannel(tcp);
/***************server***********************/
RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "chart", WellKnownObjectMode.Singleton);

/***************Client***********************/
deal.deal show = (deal.deal)Activator.GetObject(typeof(deal.deal), "tcp://localhost:8088/Chart");
while(true )
{
msg =Console.ReadLine();
show.show(msg);
}
}
}
}
server2:
static void Main(string[] args)
{
string msg = string.Empty;
TcpChannel tcp = new TcpChannel(8088);
ChannelServices.RegisterChannel(tcp);
/***************server***********************/
RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "chart", WellKnownObjectMode.Singleton);
deal.deal show = (deal.deal)Activator.GetObject(typeof(deal.deal), "tcp://localhost:8099/Chart");
while (true)
{
msg = Console.ReadLine();
show.show(msg);
}
}
class:
namespace deal
{
public class deal:System.MarshalByRefObject
{
public void show(string _message)
{
Console.WriteLine("Server:" +_message);
}
}

}
dancingbit 2009-09-12
  • 打赏
  • 举报
回复
没用过Remoting,不过我估计可能是端口的问题,一个端口只能绑定一次,你可以换其他端口再尝试去注册看看。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧