登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

Remoting in C#  

2008-03-21 14:43:27|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

By  farazfastian43  May 13, 2002

This simple article shows you how to write simple server and client to access the server remotely.

Remoting is a framework built into Common Language Runtime (CLR) in order to provide developers classes to build distributed applications and wide range of network services. Remoting provides various features such as Object Passing, Proxy Objects, Activation, Stateless and Stateful Object, Lease Based LifeTime and Hosting of Objects in IIS. I'm not going into detail of these features because it will take 3 to 4 tutorials.

Here I'm presenting a simple client/server based application in order to provide you easy and fast hands on Remoting.

Remoting Object

This is the object to be remotely accessed by network applications. The object to be accessed remotely must be derived by MarshalByRefObject and all the objects passed by value must be serializable.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemotingSamples
{
public class RemoteObject : MarshalByRefObject
{
///constructor
public RemoteObject()
{
Console.writeline("Remote object activated");
}
///return message reply
public String ReplyMessage(String msg)
{
Console.WriteLine("Client : "+msg);//print given message on console
return "Server : Yeah! I'm here";
}
}
}

The remote object must be compiled as follows to generate remoteobject.dll which is used to generate server and client executable.

csc /t:library /debug /r:System.Runtime.Remoting.dll remoteobject.cs

The Server

This is the server application used to register remote object to be access by client application. First, of all choose channel to use and register it, supported channels are HTTP, TCP and SMTP. I have used here TCP. Than register the remote object specifying its type.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemotingSamples
{
public class Server
{
///constructor
public Server()
{
}
///main method
public static int Main(string [] args)
{
//select channel to communicate
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan); //register channel
//register remote object
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("RemotingSamples.RemoteObject,object"),"RemotingServer",WellKnownObjectMode.SingleCall);
//inform console
Console.WriteLine("Server Activated");
return 0;
}
}
}

The server must be compiled as follows to produce server.exe.

csc /debug /r:remoteobject.dll /r:System.Runtime.Remoting.dll server.cs

The Client

This is the client application and it will call remote object method. First, of all client must select the channel on which the remote object is available, activate the remote object and than call proxy's object method return by remote object activation.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingSamples;
namespace RemotingSamples
{
public class Client
{
///constructor
public Client()
{
}
///main method
public static int Main(string [] args)
{
//select channel to communicate with server
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
RemoteObject remObject = (RemoteObject)Activator.GetObject(typeof RemotingSamples.RemoteObject),"tcp://localhost:8085/RemotingServer");
if (remObject==null)
Console.WriteLine("cannot locate server");
else
remObject.ReplyMessage("You there?");
return 0;
}
}
}

The client must be compiled as follows in order to produce client.exe

csc /debug /r:remoteobject.dll /r:System.Runtime.Remoting.dll client.cs

 

原文地址:http://www.c-sharpcorner.com/UploadFile/ahmadfaraz/RemotingInCsharp11242005053521AM/RemotingInCsharp.aspx

    这里有一篇翻译:http://blog.ttwv.com/html/53/t-32353.html

再追加几篇文章:

    http://www.cnblogs.com/Apex-yang/articles/854975.html

    http://www.cnblogs.com/Apex-yang/articles/854978.html

    http://www.cnblogs.com/Apex-yang/articles/854982.html

  评论这张
 
阅读(1317)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018