先贴代码:// 服务端
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代码。

解决方案 »

  1.   

    请仔细看看代码,基于.NET REMOTING的一个很简单的小程序。只不过现在想扩展一下,实现对象的双向传递
      

  2.   

    Remoting实现对象双向传递并不难,关键看你要实现哪种模式的
    一般是将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,实现起来更方便
      

  3.   

    我现在的框架用的模式类似于第一种做法,是通过Prox调用的
    但是我是每个类(对应数据库的每个表,ORM框架)都有一个独立的服务(面向服务的方式),通过透明代理实现
      

  4.   

    我现在照着网上的一个例子在改程序,但是这一句有问题.CommonService::IFaxBusiness^ watch = (CommonService::IFaxBusiness^)Activator::GetObject( Type::GetType("CommonService.CommonSvice, CommonService"), "tcp://localhost:8000/CommonSvice.soap"); CommonService::EventWrapper^ wrapper = gcnew CommonService::EventWrapper(); 
    wrapper->LocalBroadCastEvent += gcnew CommonService::BroadCastEventHandler(this, &Form1::BroadCastingMessage);
    watch->BroadCastEvent += gcnew CommonService::BroadCastEventHandler(this, &wrapper->BroadCasting);  // 这里报错:“&”: 绑定成员函数表达式上的非法操作
    另外:wangping_li,你给的文章我这两天也看过。但是不太能看懂可以麻烦一下,在我的程序上面改一下吗?
      

  5.   


    你双向通信一个DataSet没必要完全参照这个C++的啊,不要被它给锁住了
      

  6.   

    还要你现在要.net做还是c++?
    看了你这句:
    watch->BroadCastEvent += gcnew CommonService::BroadCastEventHandler(this, &wrapper->BroadCasting);  // 这里报错:“&”: 绑定成员函数表达式上的非法操作
    我都糊涂了
      

  7.   


    是C++.net啊。和C#也差不多的吧。可以帮我改一下吧。我把工程发给你
      

  8.   


    忙了一天,还没解决啊?C++.Net做这个我还没弄过,可以趁机会学习下,呵呵
      

  9.   

    是啊..我觉得不应该这么恼火才对啊...C#和C++/CLI差不多呀...很方便就可以把C#转成C++/CLI,反之也是.
      

  10.   

    远程访问的问题了,权限服务,听说过,楼主可以去搜搜
    Mark 
     up
      

  11.   

    http://www.codeproject.com/KB/dotnet/OptimizingSerialization2.aspx
    http://www.codeproject.com/KB/IP/remoting_broker.aspx
    不知道是不是楼主想要的