您的位置:首页 > 其它

.Net中Remoting通信机制简单实例

2016-11-29 23:24 330 查看

.Net中Remoting通信机制

前言:

本程序例子实现一个简单的Remoting通信案例

 

  本程序采用语言:c#

  编译工具:vs2013工程文件

  编译环境:.net 4.0

程序模块:

  • Test测试
  • Talker
  • Server端
  • Client端
  • 源代码工程文件下载

 

Test测试程序截图:

Talker类:

public class Talker : MarshalByRefObject
{
public void Talk(string word)
{
System.Console.WriteLine(word);
}

}

 

Server端:

//注册通道
TcpServerChannel channel = new TcpServerChannel("TalkChannel",8090);
ChannelServices.RegisterChannel(channel,true);

//注册远程对象
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Talker),
"Talker",
WellKnownObjectMode.SingleCall);

Client端:

public partial class Form1 : Form
{
private Talker _talk = null;
public Form1()
{
InitializeComponent();
}

private void btnSend_Click(object sender, EventArgs e)
{
if (btnSend.Text.Equals("开始"))
{
timer1.Enabled = true;
btnSend.Text = "结束";
}
else
{
timer1.Enabled = false;
btnSend.Text = "开始";
}
}

private void sendMsg(string msg)
{
try
{
//操作远程对象
_talk.Talk(msg);
string newline = msg + Environment.NewLine;
txtContent.Text = txtContent.Text.Insert(0, newline);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
timer1.Interval = 1000;
//注册通道
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, true);
//获取远程对象
_talk = (Talker)Activator.GetObject(typeof(Talker), "TCP://localhost:8090/Talker");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void timer1_Tick(object sender, EventArgs e)
{
sendMsg(txtWord.Text.Trim());
}

 

源代码工程文件下载:

  源代码工程文件下载 http://files.cnblogs.com/files/JiYF/RemotingSolution.rar

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: