失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 做开发很久了 Remoting 一直没有碰过 正好最近的项目上面用 就拿出来给大家看看

做开发很久了 Remoting 一直没有碰过 正好最近的项目上面用 就拿出来给大家看看

时间:2024-05-31 05:39:20

相关推荐

做开发很久了 Remoting 一直没有碰过 正好最近的项目上面用 就拿出来给大家看看

首先Remoting 严格来说分为3个部分, 服务端 ,客户端,中间件,中间件是指把要公布的方法和接口写成dll的形式,给客户端和服务端调用。

不说其他的了 ,直接上代码

RemotingModel 中间件 是建立的类库程序

Talker.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RemotingModel

{

public class Talker : MarshalByRefObject

{

/// <summary>

/// 说话

/// </summary>

/// <param name="word"></param>

public void Talk(string word)

{

System.Console.WriteLine(word);

}

}

}

此类中就写了一个方法, 然后要公布出去 用的 MarshalByRefObject

RemotingServer 服务端 是用的控制台程序

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.Tcp;

using RemotingModel;

namespace RemotingServer

{

class Program

{

static void Main(string[] args)

{

//RemotingConfiguration.Configure(Application.ExecutablePath);

//注册通道

TcpServerChannel channel = new TcpServerChannel("TalkChannel", 8090); //端口随便取

ChannelServices.RegisterChannel(channel, true);

//注册远程对象

RemotingConfiguration.RegisterWellKnownServiceType(

typeof(Talker),

"Talker",

WellKnownObjectMode.SingleCall);

Console.WriteLine("请不要关此程序,3Q");

Console.ReadLine();

}

}

}

RemotingClient 客户端 是winform程序

我的客户端用的是一个窗体

using System;

using System.Collections.Generic;

using ponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using RemotingModel;

namespace RemotingClient

{

public partial class Form1 : Form

{

private Talker _talk = null;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

try

{

//注册通道

TcpClientChannel channel = new TcpClientChannel();

ChannelServices.RegisterChannel(channel, true);

//获取远程对象

_talk = (Talker)Activator.GetObject(typeof(Talker), "TCP://10.4.21.121:8090/Talker");

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void btn_Send_Click(object sender, EventArgs e)

{

try

{

//操作远程对象

_talk.Talk(txtWord.Text.Trim());

txtContent.Text = "发送成功" + txtWord.Text.Trim();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

}

上面就是一个简单的remoting 结构的程序做完了,其实很简单的

代码下载 /detail/zuoming120/3696247

如果觉得《做开发很久了 Remoting 一直没有碰过 正好最近的项目上面用 就拿出来给大家看看》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。