先贴代码:// 服务端
TcpChannel^ channel = gcnew TcpChannel(8000);
ChannelServices::RegisterChannel(channel, true);
// 注册远程对象
RemotingConfiguration::ApplicationName = "RemotingApplication";
RemotingConfiguration::RegisterWellKnownServiceType( Type::GetType( "CommonService.CommonSvice, CommonService" ), "CommonSvice", WellKnownObjectMode::SingleCall );
CommonService::CommonSvice::DateSetEventHandler += gcnew CommonService::DateSetEventHandler( this, &Form1::OnDsRevice );
listBox1->CheckForIllegalCrossThreadCalls = false;//
private: System::Void OnDsRevice(DataSet^ DsRevice){ // 收到DATASET后,在LISTBOX上显示一行字符串
listBox1->Items->Add( "OK!DataSet" );
}// 客户端
TcpClientChannel^ channelClient = gcnew TcpClientChannel();
ChannelServices::RegisterChannel (channelClient, true); RemotingConfiguration::RegisterWellKnownClientType( Type::GetType("CommonService.CommonSvice, CommonService"), "tcp://localhost:8000/CommonSvice" );
obj = gcnew CommonService::CommonSvice();// 按钮按下时:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
DataSet^ DStemp = gcnew DataSet();
obj->SendDataSet(DStemp);
}
// 接口
namespace CommonService { public delegate void DateSetEventHandler(DataSet^); public interface class IFaxBusiness
{
void SendDataSet(String^);
}; public ref class CommonSvice : public MarshalByRefObject
{
// TODO: 在此处添加此类的方法
public:static event DateSetEventHandler^ DateSetEventHandler;
public: void SendDataSet(DataSet^ DsTemp)
{
DateSetEventHandler(DsTemp);
} public: virtual Object^ InitializeLifetimeService() override
{
return nullptr;
}
};
}
以上代码已经实现了:客户端向服务端发送一个DataSet,服务收到后,在listBox中显示一个字符串。
现在我希望基于以上代码,实现双向发送DataSet或其他对象。
请高手帮忙修改一下~~谢谢了。C++/CLI代码。
TcpChannel^ channel = gcnew TcpChannel(8000);
ChannelServices::RegisterChannel(channel, true);
// 注册远程对象
RemotingConfiguration::ApplicationName = "RemotingApplication";
RemotingConfiguration::RegisterWellKnownServiceType( Type::GetType( "CommonService.CommonSvice, CommonService" ), "CommonSvice", WellKnownObjectMode::SingleCall );
CommonService::CommonSvice::DateSetEventHandler += gcnew CommonService::DateSetEventHandler( this, &Form1::OnDsRevice );
listBox1->CheckForIllegalCrossThreadCalls = false;//
private: System::Void OnDsRevice(DataSet^ DsRevice){ // 收到DATASET后,在LISTBOX上显示一行字符串
listBox1->Items->Add( "OK!DataSet" );
}// 客户端
TcpClientChannel^ channelClient = gcnew TcpClientChannel();
ChannelServices::RegisterChannel (channelClient, true); RemotingConfiguration::RegisterWellKnownClientType( Type::GetType("CommonService.CommonSvice, CommonService"), "tcp://localhost:8000/CommonSvice" );
obj = gcnew CommonService::CommonSvice();// 按钮按下时:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
DataSet^ DStemp = gcnew DataSet();
obj->SendDataSet(DStemp);
}
// 接口
namespace CommonService { public delegate void DateSetEventHandler(DataSet^); public interface class IFaxBusiness
{
void SendDataSet(String^);
}; public ref class CommonSvice : public MarshalByRefObject
{
// TODO: 在此处添加此类的方法
public:static event DateSetEventHandler^ DateSetEventHandler;
public: void SendDataSet(DataSet^ DsTemp)
{
DateSetEventHandler(DsTemp);
} public: virtual Object^ InitializeLifetimeService() override
{
return nullptr;
}
};
}
以上代码已经实现了:客户端向服务端发送一个DataSet,服务收到后,在listBox中显示一个字符串。
现在我希望基于以上代码,实现双向发送DataSet或其他对象。
请高手帮忙修改一下~~谢谢了。C++/CLI代码。
一般是将RemotingServices.Marshal返回的ObjRef对象序列化传递到服务器端,然后再由服务器端反序列用以访问客户端对象
大多数我们做的服务都是只有client访问server,像上面这样的话可以实现双向通信了这里有两篇文章:1、讲得比较细,比较好懂
http://www.cnblogs.com/artech/archive/2007/03/01/660595.html2、这种做法简单些,但是实现了对象的双向传递http://www.cnblogs.com/lexus/archive/2008/08/05/1261402.htmlPS:其实如果用VS2008的话,我个人建议使用WCF,实现起来更方便
但是我是每个类(对应数据库的每个表,ORM框架)都有一个独立的服务(面向服务的方式),通过透明代理实现
wrapper->LocalBroadCastEvent += gcnew CommonService::BroadCastEventHandler(this, &Form1::BroadCastingMessage);
watch->BroadCastEvent += gcnew CommonService::BroadCastEventHandler(this, &wrapper->BroadCasting); // 这里报错:“&”: 绑定成员函数表达式上的非法操作
另外:wangping_li,你给的文章我这两天也看过。但是不太能看懂可以麻烦一下,在我的程序上面改一下吗?
你双向通信一个DataSet没必要完全参照这个C++的啊,不要被它给锁住了
看了你这句:
watch->BroadCastEvent += gcnew CommonService::BroadCastEventHandler(this, &wrapper->BroadCasting); // 这里报错:“&”: 绑定成员函数表达式上的非法操作
我都糊涂了
是C++.net啊。和C#也差不多的吧。可以帮我改一下吧。我把工程发给你
忙了一天,还没解决啊?C++.Net做这个我还没弄过,可以趁机会学习下,呵呵
Mark
up
http://www.codeproject.com/KB/IP/remoting_broker.aspx
不知道是不是楼主想要的