Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created November 24, 2010 04:19
Show Gist options
  • Save noqisofon/713106 to your computer and use it in GitHub Desktop.
Save noqisofon/713106 to your computer and use it in GitHub Desktop.
MSDN の BBS にはられたリモート関連の基本名前空間のちょっとだけ改変クライアント編。
using System;
using System.Collections.Generic;
using System.Text;
namespace moa.test.remote.client {
/// <summary>
///
/// </summary>
class MoaRemoteClientSample {
/// <summary>
///
/// </summary>
/// <param name="args"></param>
public void run(string[] args) {
Console.WriteLine( "client start" );
Console.WriteLine( AppDomain.CurrentDomain.SetupInformation.ApplicationName.ToString() );
Console.Write( "regist start, please enter " );
Console.ReadKey();
RemoteClient client = new RemoteClient();
IRemoteObject server_remote_object = client.createRemoteScreenObject( "localhost",
"remortobj",
typeof( IRemoteObject )
) as IRemoteObject;
RemoteObject client_remote_object = new RemoteObject();
client_remote_object.sendMessage += new SendMessageEventHandler( messageSend ); // たぶんクライアントでうごいてる
server_remote_object.sendMessage += new SendMessageEventHandler( client_remote_object.onMessageSend ); // たぶんサーバでうごいてる
//server_remote_object.sendMessage += new MesSendEventHandler( messageSend );<-これだけだとサーバ内で発生
Console.WriteLine( "regist messageSend success" );
Console.ReadKey();
server_remote_object.onMessageSend();
Console.ReadKey();
}
/// <summary>
///
/// </summary>
static void messageSend() {
//Console.WriteLine( AppDomain.CurrentDomain.SetupInformation.ApplicationName.ToString() );
Console.WriteLine( "クライアント待ちうけイベントだよっ!" );
}
/// <summary>
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args) {
MoaRemoteClientSample progn = new MoaRemoteClientSample();
progn.run( args );
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
namespace moa.test.remote.client {
using moa.test.remote;
/// <summary>
///
/// </summary>
class RemoteClient {
/// <summary>
///
/// </summary>
public RemoteClient() {
}
/// <summary>
/// クライアント用 RemoteScreenObject を生成します。
/// </summary>
/// <param name="server">リモート接続するサーバ名。</param>
/// <param name="obj_name">対象のオブジェクト名。</param>
/// <param name="obj_type">対象のオブジェクトタイプ。</param>
/// <returns>アクティベートされたリモートオブジェクト。</returns>
public object createRemoteScreenObject(string server,
string obj_name,
Type obj_type
) {
Hashtable channel_config = new Hashtable();
channel_config["name"] = "ipc server";
channel_config["portName"] = server;
channel_config["secure"] = true;
IChannel channel = new IpcServerChannel( channel_config, null );
ChannelServices.RegisterChannel( channel, true );
string url = string.Format( "ipc://{0}/{1}/", server, obj_name );
return Activator.GetObject( obj_type, url );
}
/// <summary>
/// クライアント用 RemoteScreenObject を生成します。
/// </summary>
/// <param name="server">リモート接続するサーバ名。</param>
/// <param name="port">使用するポート番号。</param>
/// <param name="obj_name">対象のオブジェクト名。</param>
/// <param name="obj_type">対象のオブジェクトタイプ。</param>
/// <returns>アクティベートされたリモートオブジェクト。</returns>
public object createRemoteScreenObject(string server,
int port,
string obj_name,
Type obj_type
) {
Hashtable channel_config = new Hashtable();
channel_config.Add( "name", string.Empty ); // 匿名チャネル
channel_config.Add( "authenticationMode", obj_name );
channel_config.Add( "secure", true );
// クライアントチャネルを登録します。
//TcpClientChannel channel = new TcpClientChannel( channel_config, null );
IChannel channel = new TcpServerChannel( "", 0 );
ChannelServices.RegisterChannel( channel, false );
// リモートオブジェクトを生成します。
object result = Activator.GetObject( obj_type,
string.Format( "tcp://{0}:{1}/{2}",
server,
port,
obj_name
)
);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment