Silverlight Socket通信学习笔记

开发 后端
之前说到和地图结合,所以本文的后续工作将会把Silverlight的Socket通信与ArcGIS 的地图结合,来实现一个小小的功能,而本篇则主要关于Socket的实现过程。下面就进入正题吧。

之前因为项目的关系,涉及到与服务器实时通信,比如通过GPRS将GPS的位置信息等信息发送到服务器,然后再转发给Silverlight应用程序,最后在地图上标示出实时的地理位置,查了查相关的资料,网上给出的比较好的方法就是利用Socket与服务器通信。于是这两天看了看Silverlight下的Socket通信,在此将学习的心得和实现过程作一个记录,以供相互学习和交流。

园子里关于这方面的内容已经有很多大神写过了,这里小小的推荐一下:

http://www.cnblogs.com/webabcd/archive/2008/12/22/1359551.html

因此本文的重点知识说一下具体实现的过程,细节和原理性的东西不会太多,因为本人也是新手,所以就不卖弄了。之前说到和地图结合,所以本文的后续工作将会把Silverlight的Socket通信与ArcGIS 的地图结合,来实现一个小小的功能,而本篇则主要关于Socket的实现过程。下面就进入正题吧。

一.Silverlight的Socket通信和控制台、WinForm下的Socket通信有很大的区别。

对于后两者的Socket通信,其过程就是开启端口,绑定端口,监听端口,连接,接收数据,发送数据。

而在Silverlight中则不太一样,在Silverlight中,首先是Silverlight客户端自动向943端口的服务器端发送一个“<policy-file-request/>”的语句请求,然后服务器端向客户端发送策略文件:

clientaccesspolicy.xml,例如:

  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <access-policy> 
  3.   <cross-domain-access> 
  4.     <policy> 
  5.       <allow-from> 
  6.         <domain uri="*"/> 
  7.       </allow-from> 
  8.       <grant-to> 
  9.         <socket-resource port="4502-4534" protocol="tcp"/> 
  10.       </grant-to> 
  11.     </policy> 
  12.   </cross-domain-access> 
  13. </access-policy> 

发送之后,才允许和服务器进行Socket通信,之后的过程则都是一样。

二、服务器端

2.1、Silverligh中发送策略文件服务

上面说到,Silverlight中,服务器端会向客户端发送策略文件,然后才能开始Socket通信,下面给出服务器端的发送策略文件服务的代码,该代码是在网上找的,是别人已经写好的一个类,所以在编写Silverlight 的Socket通信程序时,只要添加这个类就好了,代码如下:

  1. using System;  
  2. using System.Net.Sockets;  
  3. using System.Net;  
  4. using System.Threading;  
  5. using System.IO;  
  6. using System.Windows.Forms;  
  7.  
  8. namespace WindowsServer  
  9. {  
  10.     class PolicySocketServer  
  11.     {  
  12.         TcpListener _Listener = null;  
  13.         TcpClient _Client = null;  
  14.         static ManualResetEvent _TcpClientConnected = new ManualResetEvent(false);  
  15.         const string _PolicyRequestString = "<policy-file-request/>";  
  16.         int _ReceivedLength = 0;  
  17.         byte[] _Policy = null;  
  18.         byte[] _ReceiveBuffer = null;  
  19.  
  20.         private void InitializeData()  
  21.         {  
  22.             string policyFile = Path.Combine(Application.StartupPath, "clientaccesspolicy.xml");  
  23.             using (FileStream fs = new FileStream(policyFile, FileMode.Open))  
  24.             {  
  25.                 _Policy = new byte[fs.Length];  
  26.                 fs.Read(_Policy, 0, _Policy.Length);  
  27.             }  
  28.             _ReceiveBuffer = new byte[_PolicyRequestString.Length];  
  29.         }  
  30.         public void StartSocketServer()  
  31.         {  
  32.             InitializeData();  
  33.  
  34.             try  
  35.             {  
  36.                 _Listener = new TcpListener(IPAddress.Any, 943);  
  37.                 _Listener.Start();  
  38.                 while (true)  
  39.                 {  
  40.                     _TcpClientConnected.Reset();  
  41.                     _Listener.BeginAcceptTcpClient(new AsyncCallback(OnBeginAccept), null);  
  42.                     _TcpClientConnected.WaitOne();  
  43.                 }  
  44.             }  
  45.             catch (Exception)  
  46.             {  
  47.             }  
  48.         }  
  49.  
  50.         private void OnBeginAccept(IAsyncResult ar)  
  51.         {  
  52.             _Client = _Listener.EndAcceptTcpClient(ar);  
  53.             _Client.Client.BeginReceive(_ReceiveBuffer, 0, _PolicyRequestString.Length, SocketFlags.None,  
  54.                 new AsyncCallback(OnReceiveComplete), null);  
  55.         }  
  56.  
  57.         private void OnReceiveComplete(IAsyncResult ar)  
  58.         {  
  59.             try  
  60.             {  
  61.                 _ReceivedLength += _Client.Client.EndReceive(ar);  
  62.                 if (_ReceivedLength < _PolicyRequestString.Length)  
  63.                 {  
  64.                     _Client.Client.BeginReceive(_ReceiveBuffer, _ReceivedLength,  
  65.                         _PolicyRequestString.Length - _ReceivedLength,  
  66.                         SocketFlags.None, new AsyncCallback(OnReceiveComplete), null);  
  67.                     return;  
  68.                 }  
  69.                 string request = System.Text.Encoding.UTF8.GetString(_ReceiveBuffer, 0, _ReceivedLength);  
  70.                 if (StringComparer.InvariantCultureIgnoreCase.Compare(request, _PolicyRequestString) != 0)  
  71.                 {  
  72.                     _Client.Client.Close();  
  73.                     return;  
  74.                 }  
  75.                 _Client.Client.BeginSend(_Policy, 0, _Policy.Length, SocketFlags.None,  
  76.                     new AsyncCallback(OnSendComplete), null);  
  77.             }  
  78.             catch (Exception)  
  79.             {  
  80.                 _Client.Client.Close();  
  81.             }  
  82.             _ReceivedLength = 0;  
  83.             _TcpClientConnected.Set(); //Allow waiting thread to proceed   
  84.         }  
  85.  
  86.         private void OnSendComplete(IAsyncResult ar)  
  87.         {  
  88.             try  
  89.             {  
  90.                 _Client.Client.EndSendFile(ar);  
  91.             }  
  92.             catch (Exception)  
  93.             {  
  94.             }  
  95.             finally  
  96.             {  
  97.                 _Client.Client.Close();  
  98.             }  
  99.         }   
  100.     }  

2.2、启动策略文件服务,声明Socket,监听端口,接收数据,发送数据。

启动策略文件服务

  1. #region Start The Policy Server 验证策略文件  
  2.             PolicySocketServer StartPolicyServer = new PolicySocketServer();  
  3.             Thread th = new Thread(new ThreadStart(StartPolicyServer.StartSocketServer));  
  4.             th.IsBackground = true;  
  5.             th.Start();  
  6.             #endregion 

声明Socket,绑定端口,开始监听

  1. private void StartButton_Click(object sender, EventArgs e)  
  2.         {//创建Socket  
  3.             listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  4.             //获取主机信息  
  5.             IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());  
  6.  
  7.             //把IP和端口转换化为IPEndPoint实例,端口号取4530  
  8.             //Win7 中开启了IPV6的地址,因此0,1对应的是IPV6的地址,2,3对应IPV4地址,3对应本机的IP地址  
  9.             //XP中没有开启IPV6  
  10.             HostIPTextBox.Text = ipHostInfo.AddressList[3].ToString();  
  11.  
  12.             if (!string.IsNullOrEmpty(PortTextBox.Text))  
  13.             {  
  14.                 //获取端口号  
  15.                 int port = Convert.ToInt32(PortTextBox.Text.Trim());  
  16.                 //获得本机的IP地址  
  17.                 localEP = new IPEndPoint(ipHostInfo.AddressList[3], port);  
  18.             }  
  19.             else  
  20.             {  
  21.                //默认4530端口  
  22.                 ipAddress = IPAddress.Parse("127.0.0.1");  
  23.                 localEP = new IPEndPoint(ipHostInfo.AddressList[3], 4530);  
  24.             }  
  25.  
  26.             try  
  27.             {  
  28.                 //绑定指定的终结点  
  29.                 listener.Bind(localEP);  
  30.                 //开始监听  
  31.                 listener.Listen(10);  
  32.                 //一直循环接收客户端的消息,开启监听端口线程  
  33.                 ThreadStart threadwatchStart = new ThreadStart(WatchConnecting);  
  34.                 threadWatch = new Thread(threadwatchStart);  
  35.                 threadWatch.IsBackground = true;  
  36.                 threadWatch.Start();  
  37.             }  
  38.             catch (Exception ex)  
  39.             {  
  40.                 MessageBox.Show(ex.Data.ToString());  
  41.             }  
  42.         } 

连接端口,接收数据

  1. private void WatchConnecting()  
  2.         {  
  3.             ChangeStatue("等待Silverlight客户端连接.....");  
  4.             while (true)  //持续不断监听客户端发来的请求    
  5.             {  
  6.                 listener.BeginAccept(AcceptCallBack, listener);  
  7.                 _flipFlop.WaitOne();  
  8.             }  
  9.         } 
  1. private  void AcceptCallBack(IAsyncResult asyresult)  
  2.         {  
  3.             Socket listener = (Socket)asyresult.AsyncState;  
  4.             Socket socket = listener.EndAccept(asyresult);  
  5.             ChangeStatue("连接到Silverlight客户端....");  
  6.             _flipFlop.Set();  
  7.             var state = new StateObject();  
  8.             state.Socket = socket;  
  9.             socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReciverCallBack, state);  
  10.         } 
  1. private void ReciverCallBack(IAsyncResult asyResult)  
  2.         {  
  3.             StateObject state = (StateObject)asyResult.AsyncState;  
  4.             Socket socket = state.Socket;  
  5.             int read = socket.EndReceive(asyResult);  
  6.             if (read > 0)  
  7.             {  
  8.                 string chunk = Encoding.UTF8.GetString(state.Buffer, 0, read);  
  9.                 state.StringBuilder.Append(chunk);  
  10.                 if (state.StringBuilder.Length > 0)  
  11.                 {  
  12.                     string result = state.StringBuilder.ToString();  
  13.                     ChangeStatue("成功接收到消息:"+result);  
  14.                     ChangeReciveText(result);  
  15.                     Send(socket, SendTextBox.Text);  
  16.                     AddListItems("接收消息:"+result+"\n");  
  17.                     AddListItems("发送消息:" + SendTextBox.Text + "\n");  
  18.                 }  
  19.             }  
  20.         } 

发送数据

  1. private void Send(Socket handler, String data)  
  2.         {  
  3.             byte[] byteData = Encoding.UTF8.GetBytes(data);  
  4.             handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallBack), handler);  
  5.         }  
  6.  
  7.         private void SendCallBack(IAsyncResult asyResult)  
  8.         {  
  9.             try  
  10.             {  
  11.                 Socket handler = (Socket)asyResult.AsyncState;  
  12.                 int byteSent = handler.EndSend(asyResult);  
  13.                 if (byteSent > 0)  
  14.                 {  
  15.                     ChangeStatue("发送数据成功!");  
  16.                 }  
  17.             }  
  18.             catch (Exception ex)  
  19.             {  
  20.                 MessageBox.Show(ex.Data.ToString());  
  21.             }  
  22.         } 

StateObject类:

  1. public class StateObject  
  2.     {  
  3.         public Socket Socket;  
  4.         public StringBuilder StringBuilder = new StringBuilder();  
  5.         public const int BufferSize = 1024;  
  6.         public byte[] Buffer = new byte[BufferSize];  
  7.         public int TotalSize;  
  8.     } 

客户端:

和服务器端类似,客户端的操作包括:声明Socket,指定服务器地址和端口,连接到指定的服务器端口,发送数据,接收数据。

下面是具体的实现代码:

声明Socket

  1. private Socket socket; 

指定服务器地址和端口,开始连接

  1. private void SendButton_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             if(string.IsNullOrEmpty(IPTextBox.Text)||string.IsNullOrEmpty(PortTextBox.Text))  
  4.             {  
  5.                 MessageBox.Show ("请输入主机IP地址和端口号!");  
  6.                 return;  
  7.             }  
  8.             //ip地址  
  9.             string host=IPTextBox.Text.Trim();  
  10.             //端口号  
  11.             int port=Convert.ToInt32(PortTextBox.Text.Trim());  
  12.             //建立终结点对象  
  13.             DnsEndPoint hostEntry=new DnsEndPoint(host,port);  
  14.             //创建一个Socket对象  
  15.             socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);  
  16.             //创建Socket异步事件参数  
  17.             SocketAsyncEventArgs socketEventArg=new SocketAsyncEventArgs ();  
  18.             //将消息转化为发送的byte[]格式  
  19.             byte[]buffer=Encoding.UTF8.GetBytes(MessageTextBox.Text);  
  20.             //注册Socket完成事件  
  21.             socketEventArg.Completed+=new EventHandler<SocketAsyncEventArgs>(socketEventArg_Completed);  
  22.             //设置Socket异步事件远程终结点  
  23.             socketEventArg.RemoteEndPoint=hostEntry;  
  24.             //将定义好的Socket对象赋值给Socket异步事件参数的运行实例属性  
  25.             socketEventArg.UserToken = buffer;  
  26.             try  
  27.             {  
  28.                 socket.ConnectAsync(socketEventArg);  
  29.             }  
  30.             catch(SocketException ex)  
  31.             {  
  32.                 throw new SocketException((int)ex.ErrorCode);  
  33.             }  
  34.         } 

向服务器发送数据,并接受服务器回复的消息。

  1. private void socketEventArg_Completed(object sender, SocketAsyncEventArgs e)  
  2.         {  
  3.            //检查是否发送出错  
  4.             if (e.SocketError != SocketError.Success)  
  5.             {  
  6.                 if (e.SocketError == SocketError.ConnectionAborted)  
  7.                 {  
  8.                     Dispatcher.BeginInvoke(() => MessageBox.Show("连接超时....请重试!"));  
  9.                 }  
  10.                 else if (e.SocketError == SocketError.ConnectionRefused)  
  11.                 {  
  12.                     Dispatcher.BeginInvoke(() => MessageBox.Show("无法连接到服务器端:"+e.SocketError));  
  13.                 }else  
  14.                 {  
  15.                     Dispatcher.BeginInvoke(() => MessageBox.Show("出错了!"+e.SocketError));  
  16.                 }  
  17.                 return;  
  18.             }  
  19.            //如果连接上,则发送数据  
  20.             if (e.LastOperation == SocketAsyncOperation.Connect)  
  21.             {  
  22.                     byte[] userbytes = (byte[])e.UserToken;  
  23.                     e.SetBuffer(userbytes, 0, userbytes.Length);  
  24.                     socket.SendAsync(e);  
  25.                       
  26.             }//如果已发送数据,则开始接收服务器回复的消息  
  27.             else if (e.LastOperation == SocketAsyncOperation.Send)  
  28.             {  
  29.                 Dispatcher.BeginInvoke(() => 
  30.                 {  
  31.                     listBox1.Items.Add("客户端在" + DateTime.Now.ToShortTimeString() + ",发送消息:" + MessageTextBox.Text);  
  32.                 });  
  33.                 byte[] userbytes = new byte[1024];  
  34.                 e.SetBuffer(userbytes, 0, userbytes.Length);  
  35.                 socket.ReceiveAsync(e);  
  36.             }//接收服务器数据  
  37.             else if (e.LastOperation == SocketAsyncOperation.Receive)  
  38.             {  
  39.                 string RecevieStr = Encoding.UTF8.GetString(e.Buffer, 0, e.Buffer.Length).Replace("\0", "");  
  40.                 Dispatcher.BeginInvoke(() => 
  41.                 {  
  42.                     listBox1.Items.Add("服务器在" + DateTime.Now.ToShortTimeString() + ",回复消息:" + RecevieStr);  
  43.                 });  
  44.                 socket.Close();  
  45.             }  
  46.         } 

xaml代码:

  1. <UserControl 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     xmlns:esri="http://schemas.esri.com/arcgis/client/2009" x:Class="SilverlightSocket.MainPage" 
  7.     mc:Ignorable="d" 
  8.     d:DesignHeight="417" d:DesignWidth="530"> 
  9.  
  10.     <Grid x:Name="LayoutRoot" Background="White"> 
  11.         <Grid.ColumnDefinitions> 
  12.             <ColumnDefinition Width="0.868*"/> 
  13.             <ColumnDefinition Width="0.135*"/> 
  14.         </Grid.ColumnDefinitions> 
  15.         <Grid.RowDefinitions> 
  16.             <RowDefinition/> 
  17.         </Grid.RowDefinitions> 
  18.         <esri:Map Background="White" WrapAround="True" Grid.ColumnSpan="2"> 
  19.             <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/> 
  20.         </esri:Map> 
  21.         <StackPanel Grid.Column="1" Background="#7F094870"> 
  22.             <StackPanel.Effect> 
  23.                 <DropShadowEffect/> 
  24.             </StackPanel.Effect> 
  25.             <TextBlock x:Name="textBlock1" Text="主机IP" Grid.Column="1" Margin="5,5,0,0" Foreground="#FFE7D4E3" FontWeight="Bold" /> 
  26.             <TextBox x:Name="IPTextBox" Text="169.254.57.67" Grid.Column="1" d:LayoutOverrides="Width" Margin="5,5,0,0" HorizontalAlignment="Left"/> 
  27.             <TextBlock x:Name="textBlock2" Text="端口号" Grid.Column="1" Margin="5,5,0,0" Foreground="#FFE7D4E3" FontWeight="Bold" /> 
  28.             <TextBox x:Name="PortTextBox" Width="51" Text="4530" Grid.Column="1" Margin="5,5,0,0" HorizontalAlignment="Left"/> 
  29.             <TextBlock  x:Name="textBlock4" Text="消息记录:" Height="23" Grid.Column="1" Margin="5,5,0,0" Foreground="#FFE7D4E3" FontWeight="Bold" /> 
  30.             <ListBox  x:Name="listBox1" Grid.Column="1" Margin="5,5,0,0" Height="150" /> 
  31.             <TextBlock x:Name="textBlock3" Text="发送信息内容" Height="16" Grid.Column="1" d:LayoutOverrides="Width" Margin="5,5,0,0" Foreground="#FFE7D4E3" FontWeight="Bold" /> 
  32.             <TextBox x:Name="MessageTextBox" Grid.Column="1" Height="50" Margin="5,5,0,0" /> 
  33.             <Button Content="发送" Height="23" x:Name="SendButton" Grid.Column="1" Margin="5,5,0,0" /> 
  34.             <Button Content="清空" Height="23" x:Name="ClearButton" Grid.Column="1" Margin="5,5,0,0" /> 
  35.         </StackPanel> 
  36.     </Grid> 
  37. </UserControl> 

最后效果示意图:

服务器端:

Silverlight客户端:

后续工作中将结合地图来实现模拟实时位置的显示功能。。。。

原文链接:http://www.cnblogs.com/potential/archive/2013/01/23/2873035.html

责任编辑:张伟 来源: 博客园
相关推荐

2009-11-26 13:12:16

Silverlight

2010-03-22 10:42:37

Java Socket

2015-01-15 16:25:23

Android源码Socket通信

2011-12-13 12:32:54

JavaNIO

2010-03-22 13:25:47

Java Socket

2010-03-19 13:48:15

Java Socket

2010-03-22 09:52:30

Server Sock

2021-08-04 08:55:02

Socket Java开发

2015-09-08 10:06:18

JavaSocket编程通信

2012-02-15 10:34:29

JavaJava Socket

2010-03-18 20:13:03

Java socket

2010-01-04 16:50:04

Silverlight

2020-11-13 08:30:57

Socket

2019-12-31 20:55:13

Socket通信TCP

2012-02-15 10:26:40

JavaJava Socket

2009-08-21 11:39:58

C# Socket通信

2010-03-19 11:12:23

Java Socket

2010-03-18 20:00:35

Java socket

2023-07-25 10:42:39

鸿蒙遥控3861小车

2015-04-07 09:32:57

phpSocket通信php出现错误
点赞
收藏

51CTO技术栈公众号