对等查找器不稳定
本文关键字:不稳定 查找 对等 | 更新日期: 2023-09-27 18:33:40
这是我在Stack Overflow上的第一篇文章,因为我总是在网站上发布问题之前先在网上搜索。但是,我已经坚持使用应用程序功能超过 2 周了。
上下文: 操作系统: 视窗 8.1 目标:目前戴尔Venue 11 pro(从2014年9月1日起使用WiFi驱动程序(,但最终目标是戴尔Venue 8 pro 语言: .NET 4.5.1 (C#( 应用类型:WinRT 应用商店应用程序
目标:我想设计一个可以通过WiFi直接连接到另一个应用程序的应用程序,以便传输消息(字节数据(。我使用DistacherTimer(每4个(来处理所有PeerFinder步骤。以下是 DispatcherTimer 方法的步骤: 1 - BrowseForPeers (i use await PeerFinder.FindAllPeerAsync((;) 2 - 接受来自请求对等方的连接:打开 1 个连接,接收消息,然后关闭连接。请求对等方使用 PeerFinder 的 ConnectionRequested 事件在可观察集合中注册。 3 - 存储在队列中的 SendMessage:对于浏览的对等体中的每个对等方 =>连接、发送、关闭(一次一个(
我的问题:当我在两台平板电脑上启动应用程序时,第一条消息已成功传输。但是,我无法发送另一个,因为我遇到了许多不同的错误:连接超时、连接被拒绝、设备或资源未处于正确状态、设备未正确连接......
我不明白问题出在哪里。我尝试了很多东西。下面是一个代码示例:
//Code used for accept the connection of one peer and receive message
using (var socket = await PeerFinder.ConnectAsync(peer))
{
if (socket != null)
{
DisplayStatus("Connection Accepted...");
var message = NetworkMessage.Deserialize(await _socketHelper.ReceiveMessage(socket));
message.SenderId = peer.Id;
OnReceiveMessage(message);
socket.Dispose();
}
}
//Code used for connecting to a peer and send message
using (var socket = await PeerFinder.ConnectAsync(pPeerInformation))
{
var messageData = NetworkMessage.Serialize(pMessage);
DisplayStatus("Sending message...");
success = await _socketHelper.SendMessage(messageData, socket, pMessage.Id);
socket.Dispose();
}
//Code used for receiving the data
//pSocket is the socket defined by PeerFinder.ConnectAsync
using (var socketWriter = new Windows.Storage.Streams.DataWriter(pSocket.OutputStream))
{
using (var socketReader = new Windows.Storage.Streams.DataReader(pSocket.InputStream))
{
OnDownloadProgressChangeEvent(new DownloadProgressArgs(0, 4, "Accept connection"));
socketWriter.WriteByte(1);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(1, 4, "Waiting for message ID"));
var count = await socketReader.LoadAsync(16);
if (count == 0)
{
socketWriter.WriteByte(0);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
return new byte[0];
}
var dataId = new byte[16];
socketReader.ReadBytes(dataId);
if (IdLogs.Contains(dataId))
{
socketWriter.WriteByte(100);
OnDownloadProgressChangeEvent(new DownloadProgressArgs(4, 4, "Message already receive, closing connection"));
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
return new byte[0];
}
IdLogs.Add(dataId);
OnDownloadProgressChangeEvent(new DownloadProgressArgs(2, 4, "Accepting message"));
socketWriter.WriteByte(2);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(2, 4, "Waiting for message size"));
count = await socketReader.LoadAsync(sizeof(uint));
if (count == 0)
{
socketWriter.WriteByte(0);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
return new byte[0];
}
var size = socketReader.ReadUInt32();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(3, 4, "Size : " + size));
socketWriter.WriteByte(3);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(4, 4, "Downloading message"));
uint sum = 0;
var totalData = new List<byte>();
while (sum < size)
{
count = await socketReader.LoadAsync(size);
if (count == 0)
{
socketWriter.WriteByte(0);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
return new byte[0];
}
var tempData = new byte[count];
socketReader.ReadBytes(tempData);
sum += count;
totalData.AddRange(tempData);
OnDownloadProgressChangeEvent(new DownloadProgressArgs(sum, size, "Data downloaded"));
}
OnDownloadProgressChangeEvent(new DownloadProgressArgs(4, 4, "Download finished"));
socketWriter.WriteByte(4);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
socketReader.DetachStream();
socketWriter.DetachStream();
socketWriter.Dispose();
socketReader.Dispose();
return totalData.ToArray();
}
}
//Code used to send data
//pSocket is the socket defined by PeerFinder.ConnectAsync
using (var socketWriter = new Windows.Storage.Streams.DataWriter(pSocket.OutputStream))
{
using (var socketReader = new Windows.Storage.Streams.DataReader(pSocket.InputStream))
{
OnDownloadProgressChangeEvent(new DownloadProgressArgs(0, 4, "Waiting for connection acceptance"));
var signal = await ReadSignal(socketReader);
if (signal != 1)
{
return false;
}
OnDownloadProgressChangeEvent(new DownloadProgressArgs(1, 4, "Sending ID"));
socketWriter.WriteBytes(pId);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(1, 4, "Waiting for ID acceptance"));
signal = await ReadSignal(socketReader);
if (signal != 2)
{
return false;
}
else if (signal == 100)
{
OnDownloadProgressChangeEvent(new DownloadProgressArgs(1, 4, "Message already managed by peer"));
return true;
}
OnDownloadProgressChangeEvent(new DownloadProgressArgs(2, 4, "Sending data size"));
socketWriter.WriteUInt32((uint)pData.Length);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(2, 4, "Waiting for size acceptance"));
signal = await ReadSignal(socketReader);
if (signal != 3)
{
return false;
}
OnDownloadProgressChangeEvent(new DownloadProgressArgs(3, 4, "Sending data"));
socketWriter.WriteBytes(pData);
await socketWriter.StoreAsync();
await socketWriter.FlushAsync();
OnDownloadProgressChangeEvent(new DownloadProgressArgs(3, 4, "Waiting for data acceptance"));
signal = await ReadSignal(socketReader);
if (signal != 4)
{
return false;
}
OnDownloadProgressChangeEvent(new DownloadProgressArgs(4, 4, "Sending completed"));
socketReader.DetachStream();
socketWriter.DetachStream();
socketWriter.Dispose();
socketReader.Dispose();
return true;
}
}
请问有人有想法吗?我什至处理套接字/数据读取器/数据写入器知道我使用关键字...
我发现了问题!在我的调度程序计时器中,我调用异步函数来浏览和发送。但我没有等待(等待(这两个方法,所以它们同时执行。PeerFinder拒绝同时浏览和连接。