那我下面的代码运行时为什么会出现“信道TCP已注册”的异常?using System;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;
using HelloRemoting;
namespace RemotingSamples
{
public class Client
{
public static void Server() //客户端做为服务器
{
TcpChannel chann=new TcpChannel(11000);
ChannelServices.RegisterChannel(chann);
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("HelloRemoting.SayHello,share"),"SayHello",WellKnownObjectMode.SingleCall);
System.Console.WriteLine("按<enter>退出……");
System.Console.ReadLine();
}
static string Connect()
{
TcpChannel chan=new TcpChannel();
ChannelServices.RegisterChannel(chan);
SayHello obj=(SayHello)Activator.GetObject(typeof(SayHello),"tcp://192.168.0.22:8085/SayHello");
if (obj==null)
{
System.Console.WriteLine("不能定位到远程服务");
return "0";
}
else
{
IPHostEntry ip=Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress= ip.AddressList[0];
IPEndPoint local=new IPEndPoint(ipAddress,8085);
string var=Console.ReadLine();
string str=obj.Hello(var);
string[] arr; //连接服务器
arr=str.Split(','); //分割字符串为数组
int i;
for(i=0;i<arr.Length;i++)
Console.WriteLine(arr[i]);
return str;
}
}
public static int client(string[] args)
{
Connect();
Thread Server1=new Thread(new ThreadStart(Server)); //启动客户端服务器
Server1.Start(); //连接到服务器
return 0;
}
}
}
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;
using HelloRemoting;
namespace RemotingSamples
{
public class Client
{
public static void Server() //客户端做为服务器
{
TcpChannel chann=new TcpChannel(11000);
ChannelServices.RegisterChannel(chann);
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("HelloRemoting.SayHello,share"),"SayHello",WellKnownObjectMode.SingleCall);
System.Console.WriteLine("按<enter>退出……");
System.Console.ReadLine();
}
static string Connect()
{
TcpChannel chan=new TcpChannel();
ChannelServices.RegisterChannel(chan);
SayHello obj=(SayHello)Activator.GetObject(typeof(SayHello),"tcp://192.168.0.22:8085/SayHello");
if (obj==null)
{
System.Console.WriteLine("不能定位到远程服务");
return "0";
}
else
{
IPHostEntry ip=Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress= ip.AddressList[0];
IPEndPoint local=new IPEndPoint(ipAddress,8085);
string var=Console.ReadLine();
string str=obj.Hello(var);
string[] arr; //连接服务器
arr=str.Split(','); //分割字符串为数组
int i;
for(i=0;i<arr.Length;i++)
Console.WriteLine(arr[i]);
return str;
}
}
public static int client(string[] args)
{
Connect();
Thread Server1=new Thread(new ThreadStart(Server)); //启动客户端服务器
Server1.Start(); //连接到服务器
return 0;
}
}
}
你建一新进程时要换一个端口号,或者连接的IP不同,就可用不同的信道,
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using HelloRemoting;
namespace RemotingSamples
{
public class Client
{
public static void Server() //客户端做为服务器
{
TcpChannel chann=new TcpChannel(8089);
ChannelServices.RegisterChannel(chann);
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("HelloRemoting.SayHello,share"),"SayHello",WellKnownObjectMode.SingleCall);
System.Console.WriteLine("按<enter>退出……");
System.Console.ReadLine();
}
public static int Main(string[] args)
{
string str;
string[] arr;
TcpChannel chan=new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
SayHello obj=(SayHello)Activator.GetObject(typeof(SayHello),"tcp://192.168.18.103:8085/SayHello");
if (obj==null)
System.Console.WriteLine("不能定位到远程服务");
else
{
string var=Console.ReadLine();
str=obj.Hello(var);
arr=str.Split(','); //分割字符串为数组
int i;
for(i=0;i<arr.Length;i++)
Console.WriteLine(arr[i]);
}
Server();
return 0;
}
}
}