为什么运行时,两个应用第一次发的信息无法显示出来,后面的就正常了呢?请指教
两个代码只是端口号不同
第一个应用代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Geolocation;
using Windows.System;
using Windows.UI.Popups;
using Windows.Networking.Sockets;
using Windows.Networking;
using Windows.Storage.Streams;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace UpdTest2
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
const string LOCAL_PORT = "12200";
DatagramSocket updSocket = null;
IOutputStream outStream = null;
bool isUdpSocketInitializzed = false;
public MainPage()
{
this.InitializeComponent();
this.txtRemoteIP.Text = "127.0.0.1";
this.txtRemotePort.Text = "12201";
}
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
if (outStream==null ||
string.IsNullOrWhiteSpace(txtToSend.Text))
{
return;
}
DataWriter writer = new DataWriter(outStream);
writer.UnicodeEncoding = UnicodeEncoding.Utf8;
writer.WriteString(txtToSend.Text);
await writer.StoreAsync();
writer.DetachStream();
writer.Dispose();
await new MessageDialog("Done").ShowAsync();
txtToSend.Text = "";
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (!isUdpSocketInitializzed)
{
updSocket = new DatagramSocket();
updSocket.MessageReceived += MessageReceived;
await updSocket.BindServiceNameAsync(LOCAL_PORT);
isUdpSocketInitializzed = true;
}
base.OnNavigatedTo(e);
}
async void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
DataReader reader = args.GetDataReader();
uint len = reader.UnconsumedBufferLength;
string message = reader.ReadString(len);
string host = string.Format("{0}:{1}", args.RemoteAddress.DisplayName, args.RemotePort);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
listView.Items.Add("From" + host + " Message:" + message);
});
}
private async void btnBind_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(txtRemoteIP.Text) ||
string.IsNullOrWhiteSpace(txtRemotePort.Text))
{
return;
}
HostName remoreHostName = new HostName(txtRemoteIP.Text);
try
{
outStream = await updSocket.GetOutputStreamAsync(remoreHostName, txtRemotePort.Text)
btnSend.IsEnabled = true;
}catch(Exception ex)
{
await new MessageDialog(ex.Message.ToString()).ShowAsync();
}
}
}
}
第二个代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Geolocation;
using Windows.System;
using Windows.UI.Popups;
using Windows.Networking.Sockets;
using Windows.Networking;
using Windows.Storage.Streams;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App7
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
const string LOCAL_PORT = "12201";
DatagramSocket udpSocket = null;
IOutputStream outStream = null;
bool isUdpSocketInitializzed = false;
public MainPage()
{
this.InitializeComponent();
this.txtRemoteIP.Text = "127.0.0.1";
this.txtRemotePort.Text = "12200";
}
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
if (outStream==null ||
string.IsNullOrWhiteSpace(txtToSend.Text))
{
return;
}
DataWriter writer = new DataWriter(outStream);
writer.UnicodeEncoding = UnicodeEncoding.Utf8;
writer.WriteString(txtToSend.Text);
await writer.StoreAsync();
writer.DetachStream();
writer.Dispose();
await new MessageDialog("Done").ShowAsync();
txtToSend.Text = "";
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (!isUdpSocketInitializzed)
{
udpSocket = new DatagramSocket();
udpSocket.MessageReceived += MessageReceived;
await udpSocket.BindServiceNameAsync(LOCAL_PORT);
isUdpSocketInitializzed = true;
}
base.OnNavigatedTo(e);
}
async void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
DataReader reader = args.GetDataReader();
uint len = reader.UnconsumedBufferLength;
string message = reader.ReadString(len);
string host = string.Format("{0}:{1}", args.RemoteAddress.DisplayName, args.RemotePort);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
listView.Items.Add("From" + host + " Message:" + message);
});
}
private async void btnBind_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(txtRemoteIP.Text) ||
string.IsNullOrWhiteSpace(txtRemotePort.Text))
{
return;
}
HostName remoteHostName = new HostName(txtRemoteIP.Text);
try
{
outStream = await udpSocket.GetOutputStreamAsync(remoteHostName, txtRemotePort.Text);
btnSend.IsEnabled = true;
}catch(Exception ex)
{
await new MessageDialog(ex.Message.ToString()).ShowAsync();
}
}
}
}
------解决思路----------------------
这个太强了,UDP通讯。从来没搞过。