在Windows Store應用程序中使用Stram Socket與桌面客戶端進行通信,一直沒弄成功,總讓俺覺得很震精,怎麼會不成功呢。後來經過幾回測試發現,原來是在DataReader那裡出了問題,總算弄成了。
Stream Socket通常用於傳輸一些比較長的數據,如文件。但這裡為了使演示變得更容易理解,我傳輸了一段字符。
首先,我們用WinForm做一個服務器端。界面不復雜,目的是偵聽連接,收到傳入的客戶端連接後,向客戶端發送一條字符串消息。
處理的邏輯代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Net; using System.Net.Sockets; namespace TestServerApp { public partial class Form1 : Form { TcpListener m_Listener = null;//用於監聽鏈接 TcpClient m_Client = null;//傳入的客戶端 public Form1() { InitializeComponent(); this.btnStart.Enabled = true; this.btnStop.Enabled = false; } /// <summary> /// 向客戶端發送字符串 /// </summary> private void SendMessage(TcpClient client) { using (var stream = client.GetStream()) { byte[] buffer = Encoding.UTF8.GetBytes("奔,不停地奔,奔向傳說中的荒原;飛,不停地飛,飛向天空的那一端。"); uint len = (uint)buffer.Length; // 先發送長度 stream.Write(BitConverter.GetBytes(len), 0, sizeof(uint)); // 再發送數據 stream.Write(buffer, 0, buffer.Length); } } private async void btnStart_Click(object sender, EventArgs e) { if (this.m_Listener == null) { this.m_Listener = new TcpListener(IPAddress.Parse(this.txtAddr.Text), Convert.ToInt32(this.udPort.Value)); } this.m_Listener.Start(); this.lblMsg.Text = "監聽已開始。"; this.btnStart.Enabled = false; this.btnStop.Enabled = true; try { m_Client = await m_Listener.AcceptTcpClientAsync(); SendMessage(m_Client); } catch (SocketException se) { this.lblMsg.Text = se.Message; } catch (Exception ex) { this.lblMsg.Text = ex.Message; } } private void btnStop_Click(object sender, EventArgs e) { if (m_Listener != null) { m_Listener.Stop(); this.lblMsg.Text = "監聽已停止。"; } this.btnStart.Enabled = true; this.btnStop.Enabled = false; } } }
接著是Win8 App客戶端。
因為我們要使用網絡連接,在創建項目後,把開清單文件,切換到【功能】選項卡,把和網絡連接有關的選項勾上。
打開主頁MainPage的XAML代碼編輯器(設計視圖),簡單布局一下界面。
<Page x:Class="WCleintApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WCleintApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid Grid.Row="0" Margin="3"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Text="服務器:"/> <TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" Text="端口:"/> <TextBox x:Name="txtServer" Grid.Row="0" Grid.Column="1" Margin="2,3"/> <TextBox x:Name="txtPort" Grid.Row="1" Grid.Column="1" Margin="2,3"/> </Grid> <StackPanel Grid.Row="1" Margin="5"> <Button Content="連接" Click="onConnClick" Margin="3,8,0,12" Padding="4,2.5"/> <TextBox x:Name="txtRec" Margin="5,7,0,0" Height="200" IsReadOnly="True"/> </StackPanel> </Grid> </Page>
本欄目