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 ...
随机推荐
- CentOS命令行与shell操作(linux系管与运维二)
原创作品,转载请注明出处:https://www.cnblogs.com/sunshine5683/p/10293729.html 在上篇文章中总结了Linux常用的开机与关机以及重启的命令,今天继续 ...
- Java - Stack源码解析
Java提高篇(三一)-----Stack 在Java中Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构,它采用典型的先进后出的操作方式完成的.每一个栈都包含一个栈顶,每次出 ...
- HDU3625(SummerTrainingDay05-N 第一类斯特林数)
Examining the Rooms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Jquery封装(学习)01
1.在开发过程中,我们有时候会经常用到重复的jquey代码,最常见的是我们那里需要就再哪里复制粘贴,这样大大增加了冗余代码,维护起来也不方便.我们何不把共同的jquery代码封装起来,哪里需要就哪里调 ...
- python 类之间的关系
类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻 ...
- mysql 添加用户
CREATE USER 'user_name'@'%' IDENTIFIED BY '12345'; -- % 代表所有ip可以访问 GRANT all ON *.* TO 'user_n ...
- python短信发送
'''以云之讯平台为例:''' url = 'https://open.ucpaas.com/ol/sms/sendsms' # 账户sidsid = 'f0ad70b276a8b63eb44f415 ...
- 【读书笔记】iOS-网络-Web Service协议与风格
协议指的是在与其它系统交换结构化信息时所要遵循的一套格式,过程与规则.此外,协议定义了在传输过程中所要使用的数据格式.这样,接收系统就能正确地解释结构化信息并做出正应的回应. 1,简单对象访问协议. ...
- OSGI企业应用开发(六)细说Blueprint & Gemini Blueprint(一)
上篇文章介绍了如何使用Blueprint將Spring框架整合到OSGI应用的Bundle中,从上篇文章中我们大概了解了Blueprint与Gemini Blueprint的关系,简单的说,Bluep ...
- .net中反射与IOC容器实现
反射还是很有用的,比如IOC容器基本上都是通过反射实现的. IOC是什么 IOC:Inversion of Control 控制反转是一种是面向对象编程中的一种设计原则,用来减低计算机代码之间的耦合度 ...