Silverlight4与TCP进行通信实例,.NET TCP双向通讯(2)
里面有两个cs文件, SocketPolicy.cs Code [http://www.xueit.com] 1 public static class SocketPolicy 2 { 3 public const string Policy = @" 4 ?xml version=""1.0"" encoding =""utf-8""?5 access-poli

里面有两个cs文件,
SocketPolicy.cs

1 public static class SocketPolicy 2 { 3 public const string Policy = @" 4 <?xml version=""1.0"" encoding =""utf-8""?> 5 <access-policy> 6 <cross-domain-access> 7 <policy> 8 <allow-from> 9 <domain uri=""*"" /> 10 </allow-from> 11 <grant-to> 12 <socket-resource port=""4502-4530"" protocol=""tcp"" /> 13 </grant-to> 14 </policy> 15 </cross-domain-access> 16 </access-policy> 17 "; 18 }
还有一个文件,内容太长,这里就不给代码了,大家可以自己新建一下看看。
拷到你的项目中就可以,不用手写。
由于代码很多这里我就不全写出来了,只把需要注意的地方告诉大家,我会把整个的demo提供给大家下载。
App.config 做为这个宿主程序的灵魂,这个破config整得我都快疯掉了。这里面就是普通的wcf配置,请注意一下我写注释的地方就可以。

1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.web> 4 <compilation debug="true" /> 5 </system.web> 6 <system.serviceModel> 7 <protocolMapping> 8 <add scheme="tcp" binding="netTcpBinding"/> 9 </protocolMapping> 10 11 <bindings> 12 <customBinding> <!--注意,这里只能使用自定义的绑定,应为Silverlight4测试版并不完全技术net.tcp--> 13 <binding name="WebChat.Service.ChatService1"> 14 <binaryMessageEncoding></binaryMessageEncoding> 15 <tcpTransport></tcpTransport> 16 </binding> 17 </customBinding> 18 <netTcpBinding> 19 <binding name ="myTcpBinding" portSharingEnabled="true"></binding> 20 </netTcpBinding> 21 </bindings> 22 23 <services> 24 <service behaviorConfiguration="WebChat.Service.ChatServiceBehavior" name="WebChat.Service.ChatService"> 25 <endpoint 26 address="Service" binding="customBinding" 27 contract="WebChat.Service.IChatService" 28 bindingConfiguration="WebChat.Service.ChatService1" > 29 </endpoint> 30 <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> <!--这个一个要写的!!!暴露元数据。--> 31 <host> 32 <baseAddresses> 33 <add baseAddress="http://localhost:4504/"/> 34 <add baseAddress="net.tcp://localhost:4503/"/> 35 </baseAddresses> 36 </host> 37 </service> 38 </services> 39 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 40 <behaviors> 41 <serviceBehaviors> 42 <behavior name="WebChat.Service.ChatServiceBehavior"> 43 <serviceMetadata httpGetEnabled="true" /> 44 <serviceDebug includeExceptionDetailInFaults="false" /> 45 </behavior> 46 </serviceBehaviors> 47 </behaviors> 48 </system.serviceModel> 49 </configuration>
宿主程序启动

1 class Program 2 { 3 static void Main(string[] args) 4 { 5 PolicyServer ps = new PolicyServer(SocketPolicy.Policy); //启动943策略文件监听端口 6 7 ServiceHost host = new ServiceHost(typeof(WebChat.Service.ChatService)); 8 Console.WriteLine(); 9 host.Open(); 10 System.Console.WriteLine("聊天室服务器开始监听..."); 11 System.Console.WriteLine("按 ENTER 停止服务器监听..."); 12 System.Console.ReadLine(); 13 host.Abort(); 14 host.Close(); 15 ps.Close(); 16 } 17 }
- 上一篇:Fortran语言转.NET使用指针
- 下一篇:强大C#(wpf)开发台球游戏源码
精彩图集
精彩文章
热门标签
INSERT_INTO_
获取主机名称
js改变背景图片
值不同的列
AVL树
exls
分割大文件
Httperr.Log
广播
通信模
Banner
类型
多态
原因分析
文件夹大小
object转数组
-bash
SetLocale
用户管理
阿里云主机
证书加密
console.log
三元表达式
字符串位置
组件
sae
velocity
ARP攻击
无法复制文件
bat
常常出现的
淡入
perl
jquery解析json
Slic
马赛克
增加账户
生命周期
$符
主键
youtube视频
上传视频
通过Socket
无用
httpclient
temp
屏幕快照
所有颜色值
python线程
Linux集群
采集器
系统分享
高效随机
yii
类变量
python函数调用
集合运算符
c语言http请求
配置心得
变量的值
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229