//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的更多相关文章

  1. 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分

    原文链接 : Create an iOS Chat App using JSQMessagesViewController – Part 2 原文作者 : Mariusz Wisniewski 译者 ...

  2. 利用WCF双工模式实现即时通讯

    概述 WCF陆陆续续也用过多次,但每次都是浅尝辄止,以将够解决问题为王道,这几天稍闲,特寻了些资料看,昨晚尝试使用WCF的双工模式实现了一个简单的即时通讯程序,通过服务端转发实现客户端之间的通讯.这只 ...

  3. WCF学习之旅—HTTP双工模式(二十)

    WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...

  4. 利用WCF的双工通讯实现一个简单的心跳监控系统

    何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态,就好比医院里面的心跳监视仪一样,能够随时显示病人的心跳情况. 心跳监控的目的是什么? 与医院里面的心跳监视仪目的类似,监控程序运行状态 ...

  5. 利用WCF的双工通讯实现一个简单的心跳监控系统 z

    利用WCF的双工通讯实现一个简单的心跳监控系统 http://www.cnblogs.com/zuowj/p/5761011.html 何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态 ...

  6. WCF消息交换模式之双工通讯(Duplex)

    WCF消息交换模式之双工通讯(Duplex) 双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服 ...

  7. WCF学习之旅—TCP双工模式(二十一)

    WCF学习之旅—请求与答复模式和单向模式(十九) WCF学习之旅—HTTP双工模式(二十) 五.TCP双工模式 上一篇文章中我们学习了HTTP的双工模式,我们今天就学习一下TCP的双工模式. 在一个基 ...

  8. [SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端

    原文:[SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端 之前开发基于WinForm监控的软件,服务端基于Wcf实现,里面涉及双工模式,在客户端里面,采用心跳包机制保持与服 ...

  9. WCF服务创建与使用(双工模式)

    昨天发布了<WCF服务创建与使用(请求应答模式)>,今天继续学习与强化在双工模式下WCF服务创建与使用,步骤与代码如下. 第一步,定义服务契约(Service Contract),注意Se ...

随机推荐

  1. webapi返回泛型给easyui

    由于之前遇到的easyui调用webapi的问题. 参见 :http://blog.csdn.net/hanjun0612/article/details/51144991 所以就考虑,封装一个泛型用 ...

  2. pycharm同级目录导入问题

    在PyCharm2017中同目录下import其他模块,会出现No model named ...的报错,但实际可以运行 这是因为PyCharm不会将当前文件目录自动加入source_path. 在当 ...

  3. BUPT2017 wintertraining(15) #2 题解

    这场有点难,QAQ.补了好久(。• ︿•̀。) ,总算能写题解了(つд⊂) A. Beautiful numbers CodeForces - 55D 题意 ​ 求\([l,r](1\le l_i\l ...

  4. 金三银四:蚂蚁金服JAVA开发面试题及答案之一面(持续更新

    一面 1.自我介绍.自己做的项目和技术领域 开放题 2.项目中的监控:那个监控指标常见的有哪些? 答:CPU.内存.IO 等等.建议下载个nmon工具,里面有各个指标. 数据库:Mysql(缓存命中. ...

  5. 【转】【JLINK下载失败,STLINK下载失败万能解决方案】JLINK和STLINK都无法下载时的解决办法,此时芯片并没有报废

    很多时候都会认为这个板子无法下载了,其实不是的,有解决办法. 原因如下: 由于客户设置的晶振频率和PLL配置错误时,就会出现这种问题,下载一次程序锁住一次板子,搞的人痛不欲生. 还有错误的外设配置,S ...

  6. 洛谷 P3853 路标设置 解题报告

    P3853 路标设置 题目背景 B市和T市之间有一条长长的高速公路,这条公路的某些地方设有路标,但是大家都感觉路标设得太少了,相邻两个路标之间往往隔着相当长的一段距离.为了便于研究这个问题,我们把公路 ...

  7. wordcloud词云

    借鉴别人的一个小例子,快速生成词云的代码: from wordcloud import WordCloud f = open(u'txt/AliceEN.txt','r').read() wordcl ...

  8. Java -- JDBC 学习--通过 ResultSet 执行查询操作

    ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. 1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集. 2. ResultSet 返回的实 ...

  9. Spring的FactoryBean使用

     Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean.工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂Bean的g ...

  10. HTTP/1.1 100 Continue - I 服了 You

    今天用 c 的 socket() 模拟发送http请求:上传一张图片到服务器. 在本地测试,本地电脑: xp, iis5.1 经过半天时间对 http post file 协议的了解,代码已经写好,测 ...