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 ...
随机推荐
- Winsock API TCP/IP网络通信
通信流程如下: 1.Winsock库的装入.初始化 #pragma comment(lib,"WS2_32.lib").WSAStartup() 2.套接字的创建(服务器端是监听套 ...
- CentOS7 mini安装后没有ifconfig命令的解决办法
在CentOS 最小化mini安装后,没有ifconfig命令,此时网卡也没有启动,所以无法yum安装net-tools. 下面三步解决此问题: 1 查看网卡名称 ip addr 2 启动网卡 ifu ...
- 批处理REG学习
首先在批处理操作注册表之前,应该了解REG命令的使用方式,详情请参阅一下网址: https://www.jb51.net/article/30078.htm 从以上链接内容我们可以详细了解使用reg的 ...
- SqlSession对象之Executor
Executor是Mybatis的一个核心接口,每一个SqlSession对象都会拥有一个Executor(执行器对象):这个执行对象负责[增删改查]的具体操作,我们可以简单的将它理解为JDBC中St ...
- POJ3264(KB7-G RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K otal Submissions: 52651 Case Time Limit: 2 ...
- ThinkPHP中create()方法自动验证表单信息
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 原理: create()方法收集表单($_POST)信息并返回,同时触发表单自动验证 ...
- 【读书笔记】iOS-iOS定位
iOS提供3种不同的定位途径: 1,WiFi定位,通过查询一个WiFi路由器的地理位置信息,比较省电:iPhone,iPod touch和iPad都可以采用: 2,蜂窝式移动电话基站定位,通过移动运营 ...
- img,a,锚链接,超链接
1.图片标签:img,单标签 图片属性: src(source): 图片的来源(路径),可以放置本地图片,也可以放网上的图片的url地址 title: 当鼠标停留在图片上的时候,显示提示的文字 alt ...
- Windows应用程序进程级别统一监控实践
一般的系统级别指标监控,更多关注CPU.内存.磁盘.网络等运行情况,对应用程序运行时的进程指标关注不够,导致不能深入了解系统运行状态.本文根据笔者应用实践,探讨一下进程级别监控涉及到的监控内容以及监控 ...
- Android /data/local/tmp目录的好处
在Android中,访问data目录一般需要root权限,但是有个另外那就是/data/local/tmp目录. 注意: (1)cd /data/local/tmp可以打开这个目录,而不是一级一级目 ...