龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

Silverlight4与TCP进行通信实例,.NET TCP双向通讯(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
里面有两个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     }


精彩图集

赞助商链接