使用wcf的双工模式做的一个控制台聊天app
//wcf 服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract(CallbackContract = typeof(iMyclass))]
public interface IService1
{
[OperationContract]
string Send(string id,string pid, string str);
[OperationContract]
string Register(string id);
[OperationContract]
List<string> ALLhost();
}
[ServiceContract]
public interface iMyclass
{
[OperationContract(IsOneWay = true)]//回调函数方法必须加IsOneWay=true
void Reciver(string str);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public static Dictionary<string, iMyclass> hostdic;
public static List<string> allhost;
/// <summary>
///
/// </summary>
/// <param name="id">发送人</param>
/// <param name="pid">接受人</param>
/// <param name="str">内容</param>
/// <returns></returns>
public string Send(string id,string pid, string str)
{
try
{
foreach (var d in hostdic)
{
if (d.Key == pid)
{
d.Value.Reciver(id+"发送:"+str);
}
}
//iMyclass myclass= OperationContext.Current.GetCallbackChannel<iMyclass>();
//myclass.Reciver("你好");
return "1";
}
catch (Exception ex)
{
return ex.Message;
}
}
public List<string> ALLhost()
{
return allhost;
}
public string Register(string id)
{
if (hostdic == null)
{
hostdic = new Dictionary<string, iMyclass>();
}
if (allhost == null)
{
allhost = new List<string>();
}
iMyclass imyclass = OperationContext.Current.GetCallbackChannel<iMyclass>();
hostdic.Add(id, imyclass);
allhost.Add(id);
return id;
}
}
}
//宿主
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ServerHost
{
class Program
{
static void Main(string[] args)
{
ServiceHost serverhost = new ServiceHost(typeof(WcfService1.Service1));
serverhost.Open();
Console.WriteLine("open");
Console.ReadKey();
}
}
}
//宿主配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WcfService1.Service1"> <endpoint address="net.tcp://192.168.1.12:3721/calculatorservice"
//改为本地的ip
binding="netTcpBinding" contract="WcfService1.IService1" bindingConfiguration ="TicketBindingConfiguration"/> </service> </services> <bindings> <netTcpBinding> <binding name="TicketBindingConfiguration" openTimeout="00:10:10" receiveTimeout="00:10:10" sendTimeout="00:10:10" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> <readerQuotas maxStringContentLength="6553600" maxArrayLength="6553600" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
//客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfService1;
using System.ServiceModel;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
myclass myclass = new myclass();
// myclass.contentevent += receive;
InstanceContext callback = new InstanceContext(myclass);
// ChannelFactory<IService1> channl = new ChannelFactory<IService1>( "wcfserver"); 如果不是双工模式没有回调的话就使用这个
DuplexChannelFactory<IService1> channl = new DuplexChannelFactory<IService1>(callback, "wcfserver");
IService1 IService1 = channl.CreateChannel();
Console.WriteLine("请输入自己的用户名");
string id = Console.ReadLine();
string str = IService1.Register(id.Trim());
Console.WriteLine(str);
while (true)
{
Console.WriteLine("输入发送数据");
string info = Console.ReadLine();
Console.WriteLine("输入接受人");
string piduser = Console.ReadLine();
Console.WriteLine("发送给" + piduser.Trim() + ":" + info.Trim());
IService1.Send(id,piduser.Trim(), info.Trim());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
public class myclass : iMyclass
{
// public delegate void conten(string str);
// public event conten contentevent;
public void Reciver(string str)
{
Console.WriteLine("{0}:" + str, System.DateTime.Now);
// contentevent(str);
}
}
}
//客户端配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="wcfserver" address="net.tcp://192.168.1.12:3721/calculatorservice" //改为本地的ip binding="netTcpBinding" contract="WcfService1.IService1" bindingConfiguration ="TicketBindingConfiguration"/> </client> <bindings> <netTcpBinding> <binding name="TicketBindingConfiguration" openTimeout="00:10:10" receiveTimeout="00:10:10" sendTimeout="00:10:10" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> <readerQuotas maxStringContentLength="6553600" maxArrayLength="6553600" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
使用wcf的双工模式做的一个控制台聊天app的更多相关文章
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
原文链接 : Create an iOS Chat App using JSQMessagesViewController – Part 2 原文作者 : Mariusz Wisniewski 译者 ...
- 利用WCF双工模式实现即时通讯
概述 WCF陆陆续续也用过多次,但每次都是浅尝辄止,以将够解决问题为王道,这几天稍闲,特寻了些资料看,昨晚尝试使用WCF的双工模式实现了一个简单的即时通讯程序,通过服务端转发实现客户端之间的通讯.这只 ...
- WCF学习之旅—HTTP双工模式(二十)
WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...
- 利用WCF的双工通讯实现一个简单的心跳监控系统
何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态,就好比医院里面的心跳监视仪一样,能够随时显示病人的心跳情况. 心跳监控的目的是什么? 与医院里面的心跳监视仪目的类似,监控程序运行状态 ...
- 利用WCF的双工通讯实现一个简单的心跳监控系统 z
利用WCF的双工通讯实现一个简单的心跳监控系统 http://www.cnblogs.com/zuowj/p/5761011.html 何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态 ...
- WCF消息交换模式之双工通讯(Duplex)
WCF消息交换模式之双工通讯(Duplex) 双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服 ...
- WCF学习之旅—TCP双工模式(二十一)
WCF学习之旅—请求与答复模式和单向模式(十九) WCF学习之旅—HTTP双工模式(二十) 五.TCP双工模式 上一篇文章中我们学习了HTTP的双工模式,我们今天就学习一下TCP的双工模式. 在一个基 ...
- [SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端
原文:[SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端 之前开发基于WinForm监控的软件,服务端基于Wcf实现,里面涉及双工模式,在客户端里面,采用心跳包机制保持与服 ...
- WCF服务创建与使用(双工模式)
昨天发布了<WCF服务创建与使用(请求应答模式)>,今天继续学习与强化在双工模式下WCF服务创建与使用,步骤与代码如下. 第一步,定义服务契约(Service Contract),注意Se ...
随机推荐
- Java之JSON操作(gson)
使用gson包操作JSON数据. 依赖包:gson-2.8.2.jar <dependency> <groupId>com.google.code.gson</group ...
- selenium之测试角色管理页面举例
# 测试角色权限管理页面功能 # author:xr # date:2017-07-20 from selenium import webdriver from login_page import L ...
- codeforces278A
Circle Line CodeForces - 278A 郑州地铁的圆线有n个车站. 我们知道所有邻近站点之间的距离: d[1]是第1站和第2站之间的距离;d[2]是第2站和第3站之间的距离;… d ...
- BZOJ4808马——二分图最大独立集
题目描述 众所周知,马后炮是中国象棋中很厉害的一招必杀技."马走日字".本来,如果在要去的方向有别的棋子挡住(俗 称"蹩马腿"),则不允许走过去.为了简化问题, ...
- 睡前小dp-hdu3853-概率dp
http://acm.hdu.edu.cn/showproblem.php?pid=3853 膜猴烧酒madoka 讲定义为dp[i][j] 位置为ij的魔法值期望,可以发现dp[i][j] = dp ...
- ACM-ICPC 2018 焦作赛区网络预赛 E Jiu Yuan Wants to Eat (树链剖分+线段树)
题目链接:https://nanti.jisuanke.com/t/31714 题意:给你一棵树,初始全为0,有四种操作: 1.u-v乘x 2.u-v加x 3. u-v取反 4.询问u-v ...
- python构建bp神经网络_曲线拟合(一个隐藏层)__1.可视化数据
1.将数据写入csv文件,应该可以python代码直接实现数据集的写入,但我对文件读取这块不太熟练,等我成功了再加上,这里我直接手写将数据集写入Excel 2.然后把后缀改成.csv就可以了,利用pa ...
- #define用法之一
[问题由来] 多文件工程中,A文件要使用B文件的func1函数,只要在A文件中include “B.h”即可: 但A文件中使用的是func2函数,它与func1函数功能一样,只是名字不同而已,即fun ...
- 【原创】py3+requests+json+xlwt,爬取拉勾招聘信息
在拉勾搜索职位时,通过谷歌F12抓取请求信息 发现请求是一个post请求,参数为: 返回的是json数据 有了上面的基础,我们就可以构造请求了 然后对获取到的响应反序列化,这样就获取到了json格式的 ...
- bzoj2599/luogu4149 [IOI2011]Race (点分治)
点分治.WA了一万年. 重点就是统计答案的方法 做法一(洛谷AC bzojWA 自测WA): 做点x时记到x距离为k的边数最小值为dis[k],然后对每一对有值的dis[i]和dis[K-i],给an ...