Winfrom 使用WCF 实现双工通讯
实现双工通讯主要分三步。
- 通信接口的定义;
- 被调用接口的实现
- 双工通道的建立
请先引用DLL(CSDN的代码编辑器真尼玛蛋疼)
整个解决方案的结构
1、通信接口的定义;
服务端调用客户端接口IServerCallClient
- /// <summary>
- /// 服务器调用客户端接口
- /// </summary>
- public interface IServerCallClient
- {
- /// <summary>
- /// 服务器读取客户端的IP
- /// </summary>
- /// <returns></returns>
- [OperationContract]
- IPEndPoint ServerRequestCLientIP();
- /// <summary>
- /// 服务器发送消息给客户端
- /// </summary>
- /// <param name="text"></param>
- [OperationContract]
- void ServerSayMSG(string text);
- }
/// <summary>
/// 服务器调用客户端接口
/// </summary>
public interface IServerCallClient
{
/// <summary>
/// 服务器读取客户端的IP
/// </summary>
/// <returns></returns>
[OperationContract]
IPEndPoint ServerRequestCLientIP();
/// <summary>
/// 服务器发送消息给客户端
/// </summary>
/// <param name="text"></param>
[OperationContract]
void ServerSayMSG(string text);
}
客户端调用服务端接口IClientCallServer
- <span style="font-weight: normal;"> [ServiceContract(CallbackContract =typeof(IServerCallClient))]//指定回调的接口
- public interface IClientCallServer
- {
- /// <summary>
- /// 客户端发送消息给服务器
- /// </summary>
- /// <param name="text"></param>
- [OperationContract]//指定操作约束,不添加该接口方法不可使用
- void ClientSayToServer(string text);
- /// <summary>
- /// 客户端读取服务端时间
- /// </summary>
- /// <returns></returns>
- [OperationContract]
- DateTime ClientRequestTime();
- }</span>
<span style="font-weight: normal;"> [ServiceContract(CallbackContract =typeof(IServerCallClient))]//指定回调的接口
public interface IClientCallServer
{
/// <summary>
/// 客户端发送消息给服务器
/// </summary>
/// <param name="text"></param>
[OperationContract]//指定操作约束,不添加该接口方法不可使用
void ClientSayToServer(string text);
/// <summary>
/// 客户端读取服务端时间
/// </summary>
/// <returns></returns>
[OperationContract]
DateTime ClientRequestTime();
}</span>
上面的两个接口是单独的一个项目。
双方谈好了就好互相实现对方的方法。
2、被调用接口的实现
客户端实现服务端接口的类:
- class ServerCallClient :IServerCallClient
- {
- public IPEndPoint ServerRequestCLientIP()
- {
- var ip =new IPEndPoint(IPAddress.Any,10086);
- return ip;
- }
- public void ServerSayMSG(string text)
- {
- File.AppendAllText("收到的数据", text);
- }
- }
class ServerCallClient :IServerCallClient
{
public IPEndPoint ServerRequestCLientIP()
{
var ip =new IPEndPoint(IPAddress.Any,10086);
return ip;
}
public void ServerSayMSG(string text)
{
File.AppendAllText("收到的数据", text);
}
}
服务端实现客户端接口的类
- /// <summary>
- /// 客户回调服务器类
- /// </summary>
- [ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]//通道只建立一个除非断开才新建(SessionID是同一个)
- public class ClientCallServer : IClientCallServer, IDisposable
- {
- public static List<IServerCallClient> ServerCallClientList { get; set; }=new List<IServerCallClient>();
- /// <summary>
- /// 返回服务器时间
- /// </summary>
- /// <returns></returns>
- public DateTime ClientRequestTime()
- {
- return DateTime.Now;
- }
- /// <summary>
- /// 客户端列表
- /// </summary>
- /// <summary>
- /// 服务端向客户端发数据
- /// </summary>
- /// <param name="text"></param>
- public void ClientSayToServer(string text)
- {
- var info = OperationContext.Current;
- File.AppendAllText("receive.txt",info.SessionId+text);//收到的数据存在文件
- }
- public void Dispose()
- {
- ServerCallClientList.Clear();
- }
- #endregion
- }
/// <summary>
/// 客户回调服务器类
/// </summary>
[ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]//通道只建立一个除非断开才新建(SessionID是同一个)
public class ClientCallServer : IClientCallServer, IDisposable
{
public static List<IServerCallClient> ServerCallClientList { get; set; }=new List<IServerCallClient>();
/// <summary>
/// 返回服务器时间
/// </summary>
/// <returns></returns>
public DateTime ClientRequestTime()
{
return DateTime.Now;
}
/// <summary>
/// 客户端列表
/// </summary>
/// <summary>
/// 服务端向客户端发数据
/// </summary>
/// <param name="text"></param>
public void ClientSayToServer(string text)
{
var info = OperationContext.Current;
File.AppendAllText("receive.txt",info.SessionId+text);//收到的数据存在文件
}
public void Dispose()
{
ServerCallClientList.Clear();
}
#endregion
}
3、双工通道的建立
服务端通道的建立
- ServiceHost serviceHost = new ServiceHost(typeof(ClientCallServer));//<span style="font-family: Arial, Helvetica, sans-serif;">指定回调的类</span>
- serviceHost.AddServiceEndpoint(typeof(IClientCallServer),new WSDualHttpBinding(), "http://localhost:12345");//指定客户端的接口,通讯格式,服务器的地址
- serviceHost.BeginOpen(callback=>
- {
- serviceHost.EndOpen(callback);
- textBox1.Invoke(new Action(delegate {//不解释也能看得懂吧
- textBox1.AppendText("服务开启" + Environment.NewLine);
- }));
- },null);
ServiceHost serviceHost = new ServiceHost(typeof(ClientCallServer));//<span style="font-family: Arial, Helvetica, sans-serif;">指定回调的类</span>
serviceHost.AddServiceEndpoint(typeof(IClientCallServer),new WSDualHttpBinding(), "http://localhost:12345");//指定客户端的接口,通讯格式,服务器的地址
serviceHost.BeginOpen(callback=>
{
serviceHost.EndOpen(callback);
textBox1.Invoke(new Action(delegate {//不解释也能看得懂吧
textBox1.AppendText("服务开启" + Environment.NewLine);
}));
},null);
客户端通道的建立
- var rand = new Random();
- InstanceContext context = new InstanceContext(new ServerCallClient());//指定回调的类
- WSDualHttpBinding binding = new WSDualHttpBinding() { ClientBaseAddress = new Uri($"http://localhost:{rand.Next(10000, 20000)}") };//指定客户端地址;
- using (DuplexChannelFactory proxy = new DuplexChannelFactory(context, binding))//创建通道管理器
- {
- IClientCallServer client = proxy.CreateChannel(new EndpointAddress("http://localhost:12345"));// 创建用于将消息发送到特定终结点地址的服务的通道client.ClientRequestTime();
- client.ClientSayToServer("尼玛");
- }
Winfrom 使用WCF 实现双工通讯的更多相关文章
- 利用WCF的双工通讯实现一个简单的心跳监控系统
何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态,就好比医院里面的心跳监视仪一样,能够随时显示病人的心跳情况. 心跳监控的目的是什么? 与医院里面的心跳监视仪目的类似,监控程序运行状态 ...
- 利用WCF的双工通讯实现一个简单的心跳监控系统 z
利用WCF的双工通讯实现一个简单的心跳监控系统 http://www.cnblogs.com/zuowj/p/5761011.html 何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态 ...
- WCF双工通讯以及客户端间的间接通讯
由于学习计划安排不当,对WCF的认知一直停滞不前,最近工作上又用回了WCF,重拾一下,看到蒋老师介绍双工通讯的博文,实践一下,积累一下.原想着WCF的双工通讯就是原本的客户端能调用服务端的方法之余,服 ...
- WCF初探-5:WCF消息交换模式之双工通讯(Duplex)
双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服务端处理完请求后,返回给客户端的不是reply ...
- WCF消息交换模式之双工通讯(Duplex)
WCF消息交换模式之双工通讯(Duplex) 双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服 ...
- 浅谈WCF的三种通信模式:请求响应模式、数据报模式和双工通讯模式
一: WCF的服务端与客户端在通信时有三种模式:请求响应模式.数据报模式和双工通讯模式. 说一下基本知识, 1.如果想要将当前接口作为wcf服务器,则一定要加上[ServiceContract] 契 ...
- 一: WCF的服务端与客户端在通信时有三种模式:请求响应模式、数据报模式和双工通讯模式。
说一下基本知识, 1.如果想要将当前接口作为wcf服务器,则一定要加上[ServiceContract] 契约 2.要想将方法作为wcf服务方法发布给外部调用,则一定要加上 [Operatio ...
- wcf双工通讯
首先说一个服务引用不成功的解决办法: 需要把服务端配置文件中的Binding换成: <endpoint address="" binding="BasicHttpB ...
- wcf双工通讯遇到的问题
1.向ChannelFactory提供的InstanceContext包含未实现CallbackContractType的问题 通过添加服务引用生成的客户端代码, public class Callb ...
随机推荐
- 整数对(hdu1271)找规律
整数对 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...
- hadoop_完全分布式配置
注:此文章所写内容完全在虚拟机配置,系统:centos,jdk和hadoop已经安装完成所配集群包括hadoop01,hadoop02,hadoop03,hadoop04四台,ip分别为:192.16 ...
- Three.js开发指南---创建,加载高级网格和几何体(第八章)
本章的主要内容: 一, 通过Three.js自带的功能来组合和合并已有的几何体,创建出新的几何体 二, 从外部资源中加载网格和几何体 1 前面的章节中,我们学习到,一个几何体创建的网格,想使用多个材质 ...
- BZOJ2957: 楼房重建(分块)
题意 题目链接 Sol 自己YY出了一个\(n \sqrt{n} \log n\)的辣鸡做法没想到还能过.. 可以直接对序列分块,我们记第\(i\)个位置的值为\(a[i] = \frac{H_i}{ ...
- 关于在JSP页面用c标签写if语句
2017年5月28日,晴,心情还不错. 昨晚和同事撸串,回来后继续威士忌走起,喝到凌晨2点多,聊的甚欢.彼此分享了很多自己成长过程中的故事,相互之间有了进一步的了解,友情又进了一步.在以后的时光里,愿 ...
- 浅谈http协议六种请求方法,get、head、put、delete、post、options区别
标准Http协议支持六种请求方法,即: 1.GET 2.POST 3.PUT 4.Delete 5.HEAD 6.Options 但其实我们大部分情况下只用到了GET和POST.如果想设计一个符合RE ...
- npm install、npm init、npm update、npm uninstall和package.json
npm install 安装本地包 npm install <package_name>:这个命令将在当前目录中创建node_modules目录(如果尚不存在),并将该软件包下载到该目录. ...
- Mobileye众包地图REM的一些整理
Mobileye的CEO Shashua在CVPR2016上介绍了Road Experience Management(REM),目前仍是视觉高精度地图和定位的(几乎)唯一的解决方案.这两年间,mob ...
- linux centOS7 设置 redis 开机启动
1.为了让redis-server能在系统启动时自动运行,需要将redis服务作为守护进程(daemon)来运行,我们回/usr/local/cluster/7000/目录中找到一个redis.con ...
- 使用托管快照创建作为 Azure 托管磁盘存储的 VHD 的副本
创建快照 创建 OS 或数据磁盘 VHD 的快照,以便将其用作备份或用于排查 VM 问题. 快照是 VHD 的完整只读副本. 使用 Azure 门户创建快照 登录到 Azure 门户. 首先在左上角单 ...