在解决方案资源管理器中,需要添加两个引用:System.ServiceModel和WCFService。然后双击窗口,在Form_Load事件中编写如下代码:

  添加一个应用程序配置文件App.Config,然后粘贴如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="TcpBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
                    transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                </binding>
            </netTcpBinding>
            <wsDualHttpBinding>
                <binding name="HttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
                    transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:8000/service" binding="netTcpBinding"
                bindingConfiguration="TcpBinding" contract="ServiceReference1.IService1"
                name="TcpBinding">
                <identity>
                    <userPrincipalName value="OverBlue-PC\OverBlue" />
                </identity>
            </endpoint>
            <endpoint address="http://localhost:8001/service" binding="wsDualHttpBinding"
                bindingConfiguration="HttpBinding" contract="ServiceReference1.IService1"
                name="HttpBinding">
                <identity>
                    <userPrincipalName value="OverBlue-PC\OverBlue" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

对于这个配置文件,我是这样理解的:

1、baseAddress:由协议、地址和端口三个部分组成。其中net.tcp对应TCP协议,http对应Http协 议。

2、endPoint:该属性有一个address属性,指的是在baseAddress基础上增加address属性等于一个 完整的路径。contract则是对应程序接口,这个就不多说。而每种协议都是对应WCFService.IService1契约。

到现在,WCF宿主程序就已经建立好了,我们编译并在"非VS环境下"运行WCFHost应用程序。

三、建立客户端应用程序

  在解决方案上按右键,选择"添加" -> "新建项目",然后新建一个Windows 窗体应用程序,程序名称为"WCFClient"。0004

  然后在项目上按右键,选择"添加服务引用",在弹出的添加服务引用中,输入baseAddRess地址 :http://localhost:8001,然后点击“前往”,当确定没问题后,点击“确定”按钮。

  在客户端程序中,会自动产生一个app.config文件,双击打开该文件,我们可以在"client"段 中可以看到,net.tcp和Http两种协议属性下面都有一个"name"属性。通过这个"name"属性,我们可 以控制使用什么协议与访问服务端。

我们现在为程序添加一个按钮,双击后编写如下代码:

1
2
3
4
5
6
7
private void button1_Click(object sender, EventArgs e)
{   
    WCFClient.ServiceReference1.Service1Client sc = new
    ServiceReference1.Service1Client("TcpBinding");  
    sc.Open();   
    MessageBox.Show(sc.GetData(10));    sc.Close();
}

就个Demo这么简单就完成了。

一个WCF使用TCP协议进行通协的例子的更多相关文章

  1. WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]

    WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...

  2. WCF配置Tcp协议

    注意点: 1,<serviceMetadata httpGetEnabled="false"/>   2,       <services>         ...

  3. Java基础之UDP协议和TCP协议简介及简单案例的实现

    写在前面的废话:马上要找工作了,做了一年的.net ,到要找工作了发现没几个大公司招聘.net工程师,真是坑爹呀.哎,java就java吧,咱从头开始学呗,啥也不说了,玩命撸吧,我真可怜啊. 摘要: ...

  4. 【转】TCP协议的无消息边界问题

    http://www.cnblogs.com/eping/archive/2009/12/12/1622579.html   使用TCP协议编写应用程序时,需要考虑一个问题:TCP协议是无消息边界的, ...

  5. TCP协议漏洞影响大量Linux设备

    导读 本周三在得州奥斯丁举行的 USENIX 安全研讨会上,加州大学河滨分校研究生 Yue Cao 将报告一个严重的TCP协议边信道漏洞(PDF),该漏洞允许攻击者远程劫持任意两主机之间的会话.该漏洞 ...

  6. TCP协议的性能评测工具 — Tcpdive开源啦

    Github地址:https://github.com/fastos/tcpdive 为什么要开发Tcpdive 在过去的几年里,随着移动互联网的飞速发展,整个基础网络已经发生了翻天覆地的变化. 用户 ...

  7. 基于TCP 协议的socket 简单通信

    DNS 服务器:域名解析 socket 套接字 : ​ socket 是处于应用层与传输层之间的抽象层,也是一组操作起来非常简单的接口(接受数据),此接口接受数据之后,交由操作系统 为什么存在 soc ...

  8. 01:osi七层---基于TCP协议的套接字(socket)

    1 : osi 七层,tcp/ip 五层 1 cs架构和bs架构2 互联网3 osi七层 tcp/ip五层 -物理层   -网线.光纤        -数据链路层       -网卡        - ...

  9. 如何在wcf中用net tcp协议进行通讯

    快速阅读 如何在wcf中用net tcp协议进行通讯,一个打开Wcf的公共类.比较好好,可以记下来. 配置文件中注意配置 Service,binding,behaviors. Service中配置en ...

随机推荐

  1. centos6 多段Ip添加脚本

    #!/bin/bash export device=`ifconfig|grep eth0|head -n 1|awk '{print ($1)}'`export ipcfg_pre="/e ...

  2. Spring的AOP面向切面编程

    什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...

  3. ADO.Net连接Oracle

    1.添加 Oracle.ManagedDataAccess.dll 2.连接Oracle的实例得添加到Oracle的监听器中,不然会报“ORA-12514: TNS: 监听程序当前无法识别连接描述符中 ...

  4. SpringBoot2.0之整合Kafka

    maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  5. 算法总结之 数组的partition调整 三个值的升序

    给定一个数组arr, 其中只可能有 0,1,2三个值,请实现arr排序 另一种问法: 有一个数组,只有红 蓝 黄 球,请事先红球全放在数组的左边,蓝球放中间,黄球放右边 另一种问法: 有一个数组,再给 ...

  6. HDFS相关概念

    数据块 每个磁盘都有默认的数据块大小,这是磁盘进行数据读写的最小单位.构建与单个磁盘之上的文件系统通过磁盘块来管理该文件系统中的快.该文件系统块的大小可以使磁盘块的整数倍.文件系统块一般为几千字节,而 ...

  7. 搭建confluence参考文献

    https://www.cnblogs.com/kevingrace/p/7607442.html https://yq.aliyun.com/articles/144747?t=t1 jira: h ...

  8. review23

    文件的创建与删除 当使用File类创建一个文件对象后,例如 File file = new File("C:\\myletter", "letter.txt") ...

  9. oracle 不走索引的原因

    create table tb2 as select * from emp;alter table tb2 modify empno number(4) not null;翻到20W行 create ...

  10. JavaScript的DOM操作(节点操作)

    创建节点createElement()var node = document.createElement(“div”);没什么可说的,创建一个元素节点,但注意,这个节点不会被自动添加到文档(docum ...