求一个Remoting的例子
项目比较紧。没时间看书了,现求一个比较简单的例子。简单需求如下:
一个程序,给另一个程序(在远程计算机上运行)发送一个字符串“HELLO”,另一个程序收接之后,以MessageBox形式显示出来即可。
使用Remoting,C++/CLI代码最好。谢谢各位了。
另外....提前祝元旦快乐!
------解决方案--------------------
三个文件,一个是共用的DLL,另外就是客户端和服务器端的EXE
ClockServer.cs ,生成为DLL
using System;
public class Clock : MarshalByRefObject
{
public string GetCurrentTime ()
{
return DateTime.Now.ToLongTimeString ();
}
public void ShowMessage(String msg)
{
MessageBox.Show(msg);
}
}
TimeServer.cs 生成为服务器EXE
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
class MyApp
{
static void Main ()
{
TcpServerChannel channel = new TcpServerChannel (1234);
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterWellKnownServiceType
(typeof (Clock), "Clock", WellKnownObjectMode.SingleCall);
Console.WriteLine ("Press Enter to terminate...");
Console.ReadLine ();
}
}
TimeClient.cs 生成为客户端EXE
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
class MyApp
{
static void Main ()
{
TcpClientChannel channel = new TcpClientChannel ();
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterWellKnownClientType
(typeof (Clock), "tcp://localhost:1234/Clock");
Clock clock = new Clock ();
clock .ShowMessage("HELLO");
Console.WriteLine (clock.GetCurrentTime ());
}
}
编译之后,将ClockServer.dll分别拷贝到客户端和服务器端。
然后运行服务器,再运行客户端
------解决方案--------------------楼上就是最简单的例子!
------解决方案--------------------
参考
http://blog.csdn.net/vogts/archive/2005/05/16/375558.aspx
http://www.cnblogs.com/lovecherry/archive/2005/05/24/161437.html
------解决方案--------------------做个记号