我有一个服务器 - 客户端连接,双方都使用Windows.Networking.Sockets.StreamSocket
。在Windows 10上,作为Universal App,连接成功,数据来回流动而没有问题。在Windows 8.1上,作为Windows应用商店应用,StreamSocket的读取部分在首次尝试读取传入数据时失败。应用程序关闭,VS 2015不报告任何Exception
,输出窗口也不包含Program has exited with code 1
以外的任何有用信息。此外,设置断点然后单步执行代码并不起作用。不显示本地,VS显示消息对话框:
无法开始调试。调用的对象已与其客户端断开连接。
以下是阅读代码:
public IAsyncAction Read()
{
return Task.Run(() =>
{
try
{
const uint length = 65536;
string request = string.Empty;
var socket = _signalingSocketService.GetSocket();
var readBuf = new Windows.Storage.Streams.Buffer(length);
var readOp = socket.InputStream.ReadAsync(readBuf, length, InputStreamOptions.Partial);
readOp.Completed = (IAsyncOperationWithProgress<IBuffer, uint> asyncAction, AsyncStatus asyncStatus) =>
{
if(asyncStatus == AsyncStatus.Completed)
{
var localBuffer = asyncAction.GetResults();
var dataReader = DataReader.FromBuffer(localBuffer);
request = dataReader.ReadString(dataReader.UnconsumedBufferLength);
_signalingSocketService.HandoffSocket(socket);
List<string> requests;
var fileTask = BufferFileExists().AsTask();
fileTask.Wait();
if (fileTask.Result)
{
var bufferFileTask = GetBufferFile().AsTask();
bufferFileTask.Wait();
var bufferFile = bufferFileTask.Result;
var task = FileIO.AppendTextAsync(bufferFile, request).AsTask();
task.Wait();
var readLinesTask = FileIO.ReadLinesAsync(bufferFile).AsTask();
readLinesTask.Wait();
requests = (readLinesTask.Result).ToList();
var deleteTask = bufferFile.DeleteAsync().AsTask();
deleteTask.Wait();
}
else
{
requests =
request.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
} // if (asyncStatus == AsyncStatus.Completed)
}; // readOp.Completed
}
catch (Exception ex)
{
}
}
}
什么可能导致这种奇怪的行为?