1:創建一個工程文件,是server端的。
using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
namespace helloserver
{
/// <summary>
/// class1 的摘要說明。
/// </summary>
class hs
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main(string[] args)
{
//
// todo: 在此處添加代碼以啟動應用程序
//
try
{
tcpserverchannel channel =new tcpserverchannel(8086);
channelservices.registerchannel(channel);
type t=typeof(remotehello.hello);
remotingconfiguration.registerwellknownservicetype(t,"hi",wellknownobjectmode.singlecall);
console.writeline("hit to exit");
console.readline();
}
catch (exception ex)
{
console.writeline(ex.message);
console.writeline(ex.source);
console.readline();
}
}
}
}
2:創建一個工程文件是client端的:
using system;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
namespace helloclient
{
/// <summary>
/// class1 的摘要說明。
/// </summary>
class hc
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main(string[] args)
{
//
// todo: 在此處添加代碼以啟動應用程序
//
try
{
tcpclientchannel tc=new tcpclientchannel();
channelservices.registerchannel(tc);
remotehello.hello obj = (remotehello.hello)activator.getobject(typeof(remotehello.hello),"tcp://10.10.10.111:8086/hi");
if(obj==null)
{
console.writeline("failed!");
return;
}
for(int i=0;i<2;i++)
{
console.writeline(obj.greeting("haha"));
console.readline();
}
}
catch (exception ex)
{
console.writeline(ex.message);
console.writeline(ex.source);
console.readline();
}
}
}
}
3:在編譯完以后,先運行server.exe文件,然后再運行client.exe文件,,,這樣在ms-dos下面,就可以看到,client掉用server。。。
呵呵。。。完畢。。
新聞熱點
疑難解答
圖片精選