C# Remoting 简单实现
来源:互联网 发布:皮蓬总决赛数据 平均 编辑:程序博客网 时间:2024/06/07 01:57
今天对C# Remoting进行了初步的学习,废话不说...
RemotingModel: Talker.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
namespace RemotingModel
{
/// <summary>
///
/// </summary>
public class Talker:MarshalByRefObject
{
/// <summary>
/// 说话
/// </summary>
/// <param name="word"></param>
public void Talk(string word)
{
System.Console.WriteLine(word);
}
}
}
{
/// <summary>
///
/// </summary>
public class Talker:MarshalByRefObject
{
/// <summary>
/// 说话
/// </summary>
/// <param name="word"></param>
public void Talk(string word)
{
System.Console.WriteLine(word);
}
}
}
服务器端:是一个控制台,首先要添加对System.Runtime.Remoting的引用,然后添加对RemotingModel的引用
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingModel;
namespace RemotingServer
{
class Program
{
static void Main(string[] args)
{
//注册通道
TcpServerChannel channel = new TcpServerChannel("TalkChannel", 8090); //端口随便取
ChannelServices.RegisterChannel(channel, true);
{
class Program
{
static void Main(string[] args)
{
//注册通道
TcpServerChannel channel = new TcpServerChannel("TalkChannel", 8090); //端口随便取
ChannelServices.RegisterChannel(channel, true);
//注册远程对象
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Talker),
"Talker",
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Talker),
"Talker",
WellKnownObjectMode.SingleCall);
Console.ReadLine();
}
}
}
}
}
}
客服端:窗体:两个textBox,一个button,设置textBox为多行。上面的textBox为:txtContent,下面的为:txtWord
添加引用(添加方法同上)
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
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();
}
namespace RemotingClient
{
public partial class Form1 : Form
{
private Talker _talk = null;
public Form1()
{
InitializeComponent();
}
private void btnSend_Click(object sender, EventArgs e)
{
try
{
//操作远程对象
_talk.Talk(txtWord.Text.Trim());
txtContent.Text = "发送成功" + txtWord.Text.Trim();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
{
try
{
//操作远程对象
_talk.Talk(txtWord.Text.Trim());
txtContent.Text = "发送成功" + txtWord.Text.Trim();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
try {
//注册通道
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, true);
{
try {
//注册通道
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);
}
}
}
}
好了,下面看看结果:
_talk=(Talker) Activator.GetObject(typeof(Talker),"TCP://localhost:8090/Talker");
}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
}
}
好了,下面看看结果:
注:以上所有操作均在同一台电脑,并且在同一个解决方案执行。
接下来会跟大家分享Remoting在局域网里的使用
0 0
- C# Remoting 简单实现
- C# .NET Remoting 简单应用示例
- C# Remoting的一个简单例子
- C# Remoting的一个简单例子
- C# Remoting的一个简单例子
- C# Remoting的一个简单例子
- C# remoting
- C# Remoting
- C#采用Remoting实现跨进程调用之代码实例
- Remoting技术初探--实现简单服务器客户机通信
- C# Remoting Demo
- c# remoting 小实例
- c# Remoting分布式开发
- c# remoting 小实例
- c# remoting 小实例
- C# Remoting TcpChannel
- c# remoting调试方法
- .net Remoting简单实例
- git 命令大全
- Android Butter Knife 框架——最好用的View注入
- Zookeeper 命令和查看节点数据
- 用R语言对高维数据进行降维聚类展示
- URL encode编码和解码
- C# Remoting 简单实现
- 437. Path Sum III
- Delphi连接mysql5.1中文字符显示乱码问题:
- Ubuntu server下搭建Maven私服Nexus
- 网络问题
- 绿色软件如何设置windows自启动?
- 安卓高级8 SurfaceView (1)
- Git常用命令总结
- LeetCode 438. Find All Anagrams in a String