我认为这是由另一个应用程序使用的端口引起的,因此更改了端口号。我也关掉了防火墙,但我仍然遇到了这个例外。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
namespace Server
{
class Server
{
static void Main(string[] args)
{
TcpChannel chan = new TcpChannel(4321);
ChannelServices.RegisterChannel(chan,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DsTutorial1.Greeting),
"Greeting", WellKnownObjectMode.SingleCall);
Console.WriteLine("server is up");
Console.WriteLine("waiting for clients");
Console.ReadKey();
}
}
}
.......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DsTutorial1
{
public class Greeting:MarshalByRefObject
{
String place="ll";
String myGreeting="popo";
Greeting()
{
this.place = "LAB 3";
this.myGreeting = "hello from";
}
public String postGreeting()
{
return myGreeting+" "+place;
}
}
}
....... 单击按钮时会触发异常,并突出显示内部的行 button1_Click方法。
namespace Client
{
public partial class Form1 : Form
{
Greeting gt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TcpClientChannel chan = new TcpClientChannel();
ChannelServices.RegisterChannel(chan, false);
gt = (Greeting)Activator.GetObject(typeof(Greeting), "tcp://Localhost:4321/
Greeting");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(gt.postGreeting());
}
}
}