初学remoting,仅作笔记用
时间:2011-05-08 来源:无可比倪
实现两个进程之间的信息传输。
代码如下,远程对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemotingTest
{
public class Class1:MarshalByRefObject
{
public void otherSpeak(string content)
{
Console.WriteLine("other: " + content);
}
public string selfTalk(string content)
{
return "self: " + content;
//Console.WriteLine("other: " + content);
}
}
}
服务端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using RemotingTest;
namespace server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("开始对话: ");
//Create the server channel.
HttpChannel serverChannel = new HttpChannel(8085);
//Register the server channel.
ChannelServices.RegisterChannel(serverChannel,false);
//Expose an object for remote calls.
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "serverTalk", WellKnownObjectMode.SingleCall);
Class1 cominucationC = (Class1)Activator.GetObject(typeof(Class1), "http://localhost:8086/clientTalk");
while(true)
{
string content=Console.ReadLine();
cominucationC.otherSpeak(content);
Console.WriteLine( cominucationC.selfTalk(content));
}
}
}
}
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemotingTest;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
namespace client
{
class Program
{
static void Main(string[] args)
{
HttpChannel clientChannel = new HttpChannel(8086);
ChannelServices.RegisterChannel(clientChannel,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "clientTalk", WellKnownObjectMode.SingleCall);
//Console.Write("输入你要对话的IP: ");
//string IP = Console.ReadLine();
//Class1 cominucationS = (Class1)Activator.GetObject(typeof(Class1), "http://" + IP + ":8085/serverTalk");
Class1 cominucationS = (Class1)Activator.GetObject(typeof(Class1), "http://localhost:8085/serverTalk");
Console.WriteLine("开始对话: ");
while (true)
{
string s = Console.ReadLine();
cominucationS.otherSpeak(s);
Console.WriteLine( cominucationS.selfTalk(s));
}
}
}
}
参见:http://www.cnblogs.com/qianlifeng/archive/2011/03/10/1980113.html