Created
February 20, 2014 06:17
-
-
Save peace2048/9107976 to your computer and use it in GitHub Desktop.
Remoting の超簡単なサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Select(_ => _.ToUpper()).Any(_ => _ == "C")) | |
{ | |
RunClient(); | |
} | |
else | |
{ | |
RunServer(); | |
} | |
} | |
static void RunServer() | |
{ | |
var ch = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(18808); | |
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(ch, true); | |
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType( | |
typeof(RemoteObjImpl), | |
"sample", | |
System.Runtime.Remoting.WellKnownObjectMode.Singleton); | |
Console.WriteLine("START"); | |
Console.ReadLine(); | |
} | |
static void RunClient() | |
{ | |
var ch = new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel(); | |
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(ch, true); | |
var obj = (IRemoteObj)Activator.GetObject(typeof(IRemoteObj), "tcp://127.0.0.1:18808/sample"); | |
Console.WriteLine("1+2={0}", obj.Addition(1, 2)); | |
Console.ReadLine(); | |
} | |
} | |
#region サーバ、クライアントで参照される | |
public interface IRemoteObj | |
{ | |
int Addition(int a, int b); | |
} | |
#endregion | |
#region サーバのみで使用 | |
internal class RemoteObjImpl : MarshalByRefObject, IRemoteObj | |
{ | |
public RemoteObjImpl() | |
{ | |
Console.WriteLine("created."); | |
} | |
#region IRemoteObj メンバー | |
public int Addition(int a, int b) | |
{ | |
Console.WriteLine("called. a={0}, b={1}, result={2}", a, b, a + b); | |
return a + b; | |
} | |
#endregion | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment