有无开发大神?
@z 什么什么 @ME @LY 都在吗?
急死我了,一上午就在这里解决……
问题:我用UDP(IP四个255,端口2102)发送“0101”的包,自己接收不到?从来没有触发“Message Received”事件!
啊啊啊!
(code有点长,非重点部分请忽略)
using ATh.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
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;
// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍
namespace ATh.FirstPages
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class DeviceDiscovery : Page
{
DatagramSocket socket = null;
public DeviceDiscovery()
{
this.InitializeComponent();
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
List<DeviceClass> ReceiveList = new List<DeviceClass>();
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (socket != null)
{
socket.MessageReceived -= OnMsgReceived;
socket.Dispose();
socket = null;
}
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
socket = new DatagramSocket();
socket.Control.MulticastOnly = false;
socket.MessageReceived += OnMsgReceived;
await socket.BindServiceNameAsync("2102");
}
private async void OnMsgReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
uint stringLength = args.GetDataReader().UnconsumedBufferLength;
string receivedMessage = args.GetDataReader().ReadString(stringLength);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
if (receivedMessage != null && stringLength == 4)
{
DeviceClass DvItem = new DeviceClass { DeviceCode = receivedMessage };
DvItem.CodeToItem();
ReceiveList.Add(DvItem);
System.Diagnostics.Debug.WriteLine(DvItem.ToString());
DvItem = null;
DevicesView.ItemsSource = ReceiveList;
}
});
}
private void IsLoaded(object sender, RoutedEventArgs e)
{
StatusChecker statusChecker = new StatusChecker();
TimerCallback tcb = statusChecker.CheckStatus;
Timer stateTimer = new Timer(tcb, null, 0, 5000);
}
private void Next_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Eula));
}
private void DevicesView_ItemChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
NextButton.Content = "Next";
}
}
class StatusChecker
{
public StatusChecker()
{
}
public async void CheckStatus(object stateInfo)
{
using (DatagramSocket socket = new DatagramSocket())
{
Windows.Storage.ApplicationDataContainer localSettings =
Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
if (localSettings.Values["Node"] != null)
{
string content = (string)localSettings.Values["Node"];
HostName broardaddr = new HostName(IPAddress.Broadcast.ToString());
IOutputStream outstream = await socket.GetOutputStreamAsync(broardaddr, "2102");
DataWriter writer = new DataWriter(outstream);
writer.UnicodeEncoding = UnicodeEncoding.Utf8;
//uint len = writer.MeasureString(content);
//writer.WriteUInt32(len);
writer.WriteString(content);
await writer.StoreAsync();
writer.Dispose();
}
}
}
}
}
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!
Quoteddddefault 发表于 2016-7-22 16:05
不要用127.0.0.1
哪里有127.0.0.1了?默认我用的是255.255.255.255呀!
况且监听好像跟ip没关系啊