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
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-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配置,请注意一下我写注释的地方就可以。
Code [http://www.xueit.com]
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>
宿主程序启动
Code [http://www.xueit.com]
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)开发台球游戏源码
精彩图集
精彩文章
热门标签
CentOS5.2
soap协议
注
性能计数器
找不到IP
汉字转拼音
winrar
CHTSli
删除表
modify
校验ip
测
非成员函数
特定行
show
文档保存为
post请求
数字格式化
python模块
链接字符串
Assert
V-P交互
listview
程序员
成功
短信猫
C+
print()
ORA-27067
当前
p2p文件传输
php php中通
联想词
php php动态
jpaginate
compre
外观模式
恢复数据库
微信打开网页
加载js文件
光标定位
事务处理
comment命令
超大数据
filecmp
php解压文件
php PHP json
map
nextSibling
操作
搜索结果
内容缓存
二叉树算法
forwarded
生成随机字符
程序多次启动
$(this)
error_report
k均值算法
存取控制
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

