110,567
社区成员
发帖
与我相关
我的任务
分享
string[] arrIP = new string[] { "192.168.1.101", "192.168.2.101" }; //IP列表
int iPort =666;
int iTmp = 0;
foreach (string sIP in arrIP)
{
IDictionary tcpProperties = new Hashtable();
tcpProperties["name"] = "RemoteTest"+iPort ;
//指定服务端口
tcpProperties["port"] = iPort; //端口顺序为:666,667
tcpProperties["bindTo"] = sIP; //绑定IP
iTmp += 1;
BinaryClientFormatterSinkProvider tcpClientSinkProvider = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider tcpServerSinkProvider = new BinaryServerFormatterSinkProvider();
tcpServerSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
TcpChannel chan1 = new TcpChannel(tcpProperties, tcpClientSinkProvider, tcpServerSinkProvider);
ChannelServices.RegisterChannel(chan1); //注册信道
}
RemotingConfiguration.ApplicationName = "RemoteTest";
RemotingConfiguration.RegisterActivatedServiceType(typeof(RemoteObject.RemoteObject));
IDictionary tcpProperties = new Hashtable();
BinaryClientFormatterSinkProvider tcpClientSinkProvider = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider tcpServerSinkProvider = new BinaryServerFormatterSinkProvider();
tcpServerSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
//为防止客户端有多个网卡IP,指定使用可以与服务端通信的IP
tcpProperties["bindTo"] = "192.168.2.100";
tcpProperties["port"] = 0; //侦听所有端口
TcpChannel chan1 = new TcpChannel(tcpProperties, tcpClientSinkProvider, tcpServerSinkProvider);
//注册信道
ChannelServices.RegisterChannel(chan1);
RemoteObject.RemoteObject obj1 = (RemoteObject.RemoteObject)
Activator.CreateInstance(typeof(RemoteObject.RemoteObject), null, new object[] {
new UrlAttribute(
"tcp://192.168.2.101:667/RemoteTest" )});
string s=obj1.GetValue(""); //调用远程方法,这里就出错了。
//提示:套接字操作尝试一个无法连接的主机,192.168.1.101:666 (这个是第一个信道的地址)