我正在使用 C# 远程访问服务器系统上的标签,并且该标签的文本将通过单击客户端系统上的按钮来更改。我在一个名为 RemoteObject 的类库中创建了一个远程对象,并将这个类库的引用添加到客户端和服务器系统,但是在调试服务器系统和客户端系统时,我收到异常“每个套接字地址只有一次使用(协议/网络地址/端口)通常是允许的”
请帮助我解决这个问题..
远程对象.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RemoteObject
{
public class Class1 : MarshalByRefObject
{
public Class1()
{
}
public void setText()
{
ServerClass bs = new ServerClass();
Label lbl = bs.Controls["label1"] as Label;
lbl.Text = "New Text";
}
}
}
服务器端代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RemoteObject;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Server
{
public partial class ServerClass : Form
{
private Class1 myremoteobject;
public ServerClass()
{
InitializeComponent();
myremoteobject = new Class1();
TcpChannel channel = new TcpChannel(30000);
ChannelServices.RegisterChannel(channel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "CSBU", WellKnownObjectMode.SingleCall);
}
}
}
客户端代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RemoteObject;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Client
{
public partial class ClientClass : Form
{
private Class1 remoteobject = new Class1();
public ClientClass()
{
InitializeComponent();
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan,true);
remoteobject = (Class1)Activator.GetObject(typeof(Class1), "tcp://localhost:30000/CSBU");
}
private void changeTextBtn_Click(object sender, EventArgs e)
{
remoteobject.setText();
}
}
}
请有人尽快帮助我解决此异常。