这是我在Stack Overflow上的第一篇文章,因为我总是在网站上发布问题之前搜索网页。但是,我已经坚持使用应用功能超过2周了。
上下文: 操作系统:Windows 8.1 目标:目前戴尔Venue 11专业版(2014年9月1日推出WiFi驱动程序),但最终目标是戴尔Venue 8专业版 语言:.NET 4.5.1(C#) 应用类型:WinRT商店应用
目标: 我想设计一个可以通过WiFi Direct连接到另一个应用程序的应用程序,以便传输消息(字节数据)。 我使用DistacherTimer(每4个)处理所有PeerFinder步骤。 以下是DispatcherTimer方法的步骤: 1 - BrowseForPeers(我使用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;
}
}
有人有想法吗?我甚至配置socket / datareader / datawriter知道我使用关键字......
答案 0 :(得分:0)
我发现了这个问题! 在我的调度程序计时器中,我调用异步函数来浏览和发送。但我不等待(等待)这两种方法,所以它们同时执行。 PeerFinder拒绝同时浏览和连接。