Windows Phone开发(46):与Socket有个约会 转:http://blog.csdn.net/tcjiaan/article/details/7669315...

不知道大家有没有“谈Socket色变”的经历?就像我一位朋友所说的,Socket这家伙啊,不得已而用之。哈,Socket真的那么恐怖吗?

其实这话一点也不假,Socket有时候真的不太好操控,也不好维护,但不管怎么样,我们还是要面对它的,没准Socket是一位大美女哦。

关于Socket的前世今生就不用我详述了,关于她的历史,已经不少人仁志士为她立传写著了,像我们国内的百度百科、互动百科等;全球著名的如维基百科之属。而且,能加入WP开发的学习行列的,我想各位对.NET的其它技术肯定是有一定基础的。我也相信,各位同仁过去一定也写过与Socket打交道的程序。那么,WP中的Socket又将如何呢?

前提公布答案吧,在WP中使用Socket跟你在其它桌面应用项目如WinForm,WPF等中是一样的,而且说白了,WP中的Socket只不过是从Silverlight框架中继承过来的。

.NET的一大优势就是集成性和统一性都好,这不,你看,无论你是编写桌面应用程序,还是WP上的应用程序,你会发现,你的学习成本不高,很多东西都是一样的,而且是相通的。显然这也是Win8和WP8的应用程序可以整合的原因吧。

在WP中使用Socket要注意以下几点:

1、WP客户端应用程序一般不被视为服务器端,因为不能进行绑定本地终结点和监听连接。但是,收发数据是没问题D。

2、在WP中的Socket操作(连接、接收以及发送)都是异步进行的。如果希望UI线程和后前线程进行同步,不妨使用System.Threading.ManualResetEvent类,这个东西不好讲述,也不好理解。这样吧,我举一个例子。

有一天,NC和脑残因为一件小事闹冲突,闹来闹去还是不能解决,怎么办呢?于是,NC和脑残决定来一场比试。两人约定以跑步方式比试,谁跑得快谁就是胜利者。然而,NC这个人一向比较自负,他相信脑残绝对跑不过他。这样,NC就修改了比赛规则:

NC让脑残先跑5秒,然后他才开始。

假设NC是主线程,脑残是后台线程,现在的情况是:主线程先等待一会儿,让后台线程先执行;后台线程执行5秒后向主线程发出信号,主线程收到信号后再继续往下执行。按照故事里的情节:NC先让脑残跑5秒钟,他自己就在起跑线上等待,脑残跑了5秒后向NC发出信号,NC看到信号后就开始跑。

下面介绍一个类——SocketAsyncEventArgs。

这个类作为启动异步操作时传递的参数,它可以包含如接收数据的缓冲区、远程主机、用户自定义对象等内容,这个类并不复杂,打开“对象浏览器”看看就一目了然了。

要设置用于异步接收数据的缓冲区,应调用SetBuffer方法。

好,理论就扯到这儿,其实也没有什么新的知识点,我只是简单提一下罢了。

按照惯例,大家都会猜到,理论过后要干什么了,是的,付诸实践。

 

在很多情况下,关于Socket的例子,都会做一个聊天程序的,不过,聊天程序要求服务器端和客户都具有发送和接收数据的功能,这样会增加实例的难度和代码长度,不方便入门者阅读。所以,想了一下,今天咱们不玩聊天的,今天咱们玩遥控飞机,如何?

程序代码较长,也不便于逐一来讲解,这样吧,为了保持代码的可读性,我会把完整的代码都贴出来,在代码中我会适当地加上注释。

先说一下原理,利用Socket进行通讯这不用说了,那是肯定的。功能是通过WP手机客户端应用程序来控制PC端播放、暂停和停止动画,而动画嘛,也不弄那么复杂了,就弄个矩形从左边移到右边的动画吧。

 

第一部分  服务器端

既然要播放动画,少不了要用WPF了,而且,也方便贴界面布局的代码。

1、新建WPF应用程序项目。

2、打开MainWindow.xaml文件(默认新建项目后自动打开),输入以下XAML代码。

[html]  view plain copy print ?
  1. <Window x:Class="MYServer.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="服务器端" Height="350" Width="525">  
  5.       
  6.     <Window.Resources>  
  7.         <Storyboard x:Key="std">  
  8.             <DoubleAnimation Duration="0:0:5"  
  9.                                  Storyboard.TargetName="rect"  
  10.                                  Storyboard.TargetProperty="(Rectangle.RenderTransform).(TranslateTransform.X)"  
  11.                                  To="400"/>  
  12.         </Storyboard>  
  13.     </Window.Resources>  
  14.       
  15.     <Grid>  
  16.         <Grid.RowDefinitions>  
  17.             <RowDefinition />  
  18.             <RowDefinition Height="Auto" />  
  19.         </Grid.RowDefinitions>  
  20.         <Rectangle x:Name="rect" Grid.Row="0" Width="50" Height="50" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Center">  
  21.             <Rectangle.RenderTransform>  
  22.                 <TranslateTransform X="0" Y="0"/>  
  23.             </Rectangle.RenderTransform>  
  24.         </Rectangle>  
  25.         <TextBlock Name="txtDisplay" Grid.Row="1"/>  
  26.     </Grid>  
  27. </Window>  


3、打开MainWindow.xaml.cs文件,完成后台代码逻辑。

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14.   
  15. using System.Windows.Media.Animation;  
  16. using System.IO;  
  17. using System.Net;  
  18. using System.Net.Sockets;  
  19.   
  20.   
  21. namespace MYServer  
  22. {  
  23.     /// <summary>  
  24.     /// MainWindow.xaml 的交互逻辑  
  25.     /// </summary>  
  26.     public partial class MainWindow : Window  
  27.     {  
  28.         Storyboard std = null//演示图板  
  29.         public MainWindow()  
  30.         {  
  31.             InitializeComponent();  
  32.   
  33.             // 从资源中把Key为std的Storyboard读出来  
  34.             std = this.Resources["std"as Storyboard;  
  35.             // 声明用于监听连接请求的Socket  
  36.             Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  37.             IPEndPoint local = new IPEndPoint(IPAddress.Any, 1377); //监听所有网络接口上的地址  
  38.             Server.Bind(local);// 绑定本地终结点  
  39.             Server.Listen(100);// 侦听连接请求  
  40.             // 开始异步接受传入的连接请求  
  41.             Server.BeginAccept(new AsyncCallback(this.AcceptSocketCallback), Server);  
  42.         }  
  43.   
  44.         /// <summary>  
  45.         /// 接受传入的Socket的回调  
  46.         /// </summary>  
  47.         private void AcceptSocketCallback(IAsyncResult ia)  
  48.         {  
  49.             Socket _socket = ia.AsyncState as Socket;  
  50.             Socket accptSocket = _socket.EndAccept(ia);  
  51.             try  
  52.             {  
  53.                 IPEndPoint remote = (IPEndPoint)accptSocket.RemoteEndPoint;  
  54.                 // 显示客户端的IP  
  55.                 Dispatcher.BeginInvoke(new Action<string>(this.SetIPForText), remote.Address.ToString());  
  56.                 StateObject so = new StateObject();  
  57.                 so.theSocket = accptSocket;  
  58.                 // 开始异步接收消息  
  59.                 accptSocket.BeginReceive(so.Buffer, 0, so.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), so);  
  60.             }  
  61.             catch  
  62.             {  
  63.   
  64.             }  
  65.             // 继续接受连接请求  
  66.             _socket.BeginAccept(new AsyncCallback(this.AcceptSocketCallback), _socket);  
  67.         }  
  68.         /// <summary>  
  69.         /// 接收消息的回调  
  70.         /// </summary>  
  71.         private void ReceiveCallback(IAsyncResult ia)  
  72.         {  
  73.             StateObject _so = ia.AsyncState as StateObject;  
  74.             Socket _socket = _so.theSocket;  
  75.             try  
  76.             {  
  77.                 int n = _socket.EndReceive(ia);//n就是接收到的字节数  
  78.                 string msg = Encoding.UTF8.GetString(_so.Buffer, 0, n);  
  79.                 // 判断客户端发送了啥命令  
  80.                 switch (msg)  
  81.                 {  
  82.                     case "play":  
  83.                         Dispatcher.BeginInvoke(new Action(this.Play), null);  
  84.                         break;  
  85.                     case "pause":  
  86.                         Dispatcher.BeginInvoke(new Action(this.Pause), null);  
  87.                         break;  
  88.                     case "stop":  
  89.                         Dispatcher.BeginInvoke(new Action(this.Stop), null);  
  90.                         break;  
  91.                     default:  
  92.                         break;  
  93.                 }  
  94.             }  
  95.             catch   
  96.             {  
  97.             }  
  98.             _so = new StateObject();  
  99.             _so.theSocket = _socket;  
  100.             // 继续接收消息  
  101.             _socket.BeginReceive(_so.Buffer,  
  102.                                 0,  
  103.                                 _so.Buffer.Length,  
  104.                                 SocketFlags.None,  
  105.                                 new AsyncCallback(this.ReceiveCallback),  
  106.                                 _so);  
  107.         }  
  108.         /// <summary>  
  109.         /// 显示客户端的IP  
  110.         /// </summary>  
  111.         private void SetIPForText(string ip)  
  112.         {  
  113.             this.txtDisplay.Text = "客户端IP:" + ip;  
  114.         }  
  115.  
  116.         #region 控制动画的方法  
  117.         private void Play()  
  118.         {  
  119.             std.Begin();  
  120.         }  
  121.         private void Pause()  
  122.         {  
  123.             std.Pause();  
  124.         }  
  125.         private void Stop()  
  126.         {  
  127.             std.Stop();  
  128.         }  
  129.         #endregion  
  130.     }  
  131.   
  132.     /// <summary>  
  133.     /// 用于异步Socket操作传递的状态对象  
  134.     /// </summary>  
  135.     public class StateObject  
  136.     {  
  137.         private const int BUFFER_SIZE = 512;  
  138.   
  139.         public byte[] Buffer { get;  set; }  
  140.   
  141.         public Socket theSocket { getset; }  
  142.   
  143.         /// <summary>  
  144.         /// 构造函数  
  145.         /// </summary>  
  146.         public StateObject()  
  147.         {  
  148.             this.Buffer = new byte[BUFFER_SIZE];  
  149.         }  
  150.     }  
  151. }  


 

 

第二部分  WP客户端

1、新建Windows Phone应用程序项目。

2、打开MainPage.xaml文件,参考下面的XAML代码。

[html]  view plain copy print ?
  1. <phone:PhoneApplicationPage   
  2.     x:Class="WPClient.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.   
  16.     <!--LayoutRoot 是包含所有页面内容的根网格-->  
  17.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  18.         <Grid.RowDefinitions>  
  19.             <RowDefinition Height="Auto"/>  
  20.             <RowDefinition Height="*"/>  
  21.         </Grid.RowDefinitions>  
  22.   
  23.         <!--TitlePanel 包含应用程序的名称和页标题-->  
  24.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  25.             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>  
  26.             <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  27.         </StackPanel>  
  28.   
  29.         <!--ContentPanel - 在此处放置其他内容-->  
  30.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  31.             <Grid.RowDefinitions>  
  32.                 <RowDefinition Height="auto"/>  
  33.                 <RowDefinition Height="*"/>  
  34.             </Grid.RowDefinitions>  
  35.             <Grid Grid.Row="0">  
  36.                 <Grid.ColumnDefinitions>  
  37.                     <ColumnDefinition Width="Auto" />  
  38.                     <ColumnDefinition />  
  39.                     <ColumnDefinition Width="Auto" />  
  40.                 </Grid.ColumnDefinitions>  
  41.                 <TextBlock Grid.Column="0" VerticalAlignment="Center" Text="服务器IP:" />  
  42.                 <TextBox Name="txtServerIP" Grid.Column="1"/>  
  43.                 <Button Grid.Column="2" Content="连接" Click="onConnect"/>  
  44.             </Grid>  
  45.             <StackPanel Grid.Row="1">  
  46.                 <Button Content="放播动画" Click="onPlay"/>  
  47.                 <Button Content="暂停动画" Click="onPause"/>  
  48.                 <Button Content="停止动画" Click="onStop"/>  
  49.                 <TextBlock Name="txtbInfo" Margin="3,18,3,0"/>  
  50.             </StackPanel>  
  51.         </Grid>  
  52.     </Grid>  
  53.    
  54.     <!--演示 ApplicationBar 用法的示例代码-->  
  55.     <!--<phone:PhoneApplicationPage.ApplicationBar>  
  56.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">  
  57.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>  
  58.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>  
  59.             <shell:ApplicationBar.MenuItems>  
  60.                 <shell:ApplicationBarMenuItem Text="菜单项 1"/>  
  61.                 <shell:ApplicationBarMenuItem Text="菜单项 2"/>  
  62.             </shell:ApplicationBar.MenuItems>  
  63.         </shell:ApplicationBar>  
  64.     </phone:PhoneApplicationPage.ApplicationBar>-->  
  65.   
  66. </phone:PhoneApplicationPage>  


3、打开MainPage.xaml.cs,输入以下代码。

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13.   
  14. using System.Net.Sockets;  
  15. using System.IO;  
  16. using System.Threading;  
  17.   
  18. namespace WPClient  
  19. {  
  20.     public partial class MainPage : PhoneApplicationPage  
  21.     {  
  22.         Socket mySocket = null;  
  23.         ManualResetEvent MyEvent = null;  
  24.         // 构造函数  
  25.         public MainPage()  
  26.         {  
  27.             InitializeComponent();  
  28.         }  
  29.   
  30.         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
  31.         {  
  32.             base.OnNavigatedTo(e);  
  33.   
  34.             if (mySocket == null)  
  35.             {  
  36.                 mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  37.             }  
  38.             if (MyEvent == null)  
  39.             {  
  40.                 MyEvent = new ManualResetEvent(false);  
  41.             }  
  42.         }  
  43.   
  44.         protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)  
  45.         {  
  46.             if (mySocket != null)  
  47.             {  
  48.                 mySocket.Shutdown(SocketShutdown.Both);  
  49.                 mySocket.Close();  
  50.             }  
  51.             base.OnNavigatedFrom(e);  
  52.         }  
  53.   
  54.         private void onConnect(object sender, RoutedEventArgs e)  
  55.         {  
  56.             if (mySocket != null)  
  57.             {  
  58.                 SocketAsyncEventArgs connArg = new SocketAsyncEventArgs();  
  59.                 // 要连接的远程服务器  
  60.                 connArg.RemoteEndPoint = new DnsEndPoint(this.txtServerIP.Text, 1377);  
  61.                 // 操作完成后的回调  
  62.                 connArg.Completed += (sendObj, arg) =>  
  63.                 {  
  64.                     if (arg.SocketError == SocketError.Success) //连接成功  
  65.                     {  
  66.                         Dispatcher.BeginInvoke(() => txtbInfo.Text = "连接成功。");  
  67.                     }  
  68.                     else  
  69.                     {  
  70.                         Dispatcher.BeginInvoke(() =>  
  71.                         {  
  72.                             txtbInfo.Text = "连接失败,错误:" + arg.SocketError.ToString();  
  73.                         });  
  74.                     }  
  75.                     // 向调用线程报告操作结束  
  76.                     MyEvent.Set();  
  77.                 };  
  78.                 // 重置线程等待事件  
  79.                 MyEvent.Reset();  
  80.                 txtbInfo.Text = "正在连接,请等候……";  
  81.                 // 开始异连接  
  82.                 mySocket.ConnectAsync(connArg);  
  83.                 // 等待连接完成  
  84.                 MyEvent.WaitOne(6000);  
  85.             }  
  86.         }  
  87.   
  88.         private void onPause(object sender, RoutedEventArgs e)  
  89.         {  
  90.             SendCommand("pause");  
  91.         }  
  92.   
  93.         private void onStop(object sender, RoutedEventArgs e)  
  94.         {  
  95.             SendCommand("stop");  
  96.         }  
  97.   
  98.         private void onPlay(object sender, RoutedEventArgs e)  
  99.         {  
  100.             SendCommand("play");  
  101.         }  
  102.   
  103.   
  104.         private void SendCommand(string txt)  
  105.         {  
  106.             if (mySocket != null && mySocket.Connected)  
  107.             {  
  108.                 SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();  
  109.                 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txt);  
  110.                 sendArg.SetBuffer(buffer, 0, buffer.Length);  
  111.                 // 发送完成后的回调  
  112.                 sendArg.Completed += (objSender, mArg) =>  
  113.                     {  
  114.                         // 如果操作成功  
  115.                         if (mArg.SocketError == SocketError.Success)  
  116.                         {  
  117.                             Dispatcher.BeginInvoke(() => txtbInfo.Text = "发送成功。");  
  118.                         }  
  119.                         else  
  120.                         {  
  121.                             Dispatcher.BeginInvoke(() =>  
  122.                                 {  
  123.                                     this.txtbInfo.Text = "发送失败,错误:" + mArg.SocketError.ToString();  
  124.                                 });  
  125.                         }  
  126.                         // 报告异步操作结束  
  127.                         MyEvent.Set();  
  128.                     };  
  129.                 // 重置信号  
  130.                 MyEvent.Reset();  
  131.                 txtbInfo.Text = "正在发送,请等候……";  
  132.                 // 异步发送  
  133.                 mySocket.SendAsync(sendArg);  
  134.                 // 等待操作完成  
  135.                 MyEvent.WaitOne(6000);  
  136.             }  
  137.         }  
  138.     }  
  139. }  


 

先运行服务器端,再在WP模拟器或真实手机上运行客户端。

在手机客户端中,输入IP地址,点“连接”,连接成功后,就可以发送指令了。

 

 好的,就到这儿吧,示例的源码我会上专到“资源”中,有需要的话,大家可以按标题下载。

转载于:https://www.cnblogs.com/songtzu/archive/2012/07/24/2607192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值