Windows 8消费者预览版+Visual Studio 11开发人员预览版中的Socket BUG
本文关键字:BUG Socket Studio 消费者 +Visual Windows 开发 | 更新日期: 2023-09-27 18:27:53
我正在Visual Studio 11开发者预览版中编写一个应用程序,在该应用程序与阅读器一起运行一段时间后,我收到了此错误。InputStreamOptions=InputStreamOptions.Partial;选项集:
An unhandled exception of type 'System.Exception' occurred in mscorlib.dll
Additional information: The operation attempted to access data outside the valid range (Exception from HRESULT: 0x8000000B)
如果未设置该选项,套接字可以很好地读取流。
这是供参考的代码:
private StreamSocket tcpClient;
public string Server = "10.1.10.64";
public int Port = 6000;
VideoController vCtrl = new VideoController();
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
tcpClient = new StreamSocket();
Connect();
this.InitializeComponent();
this.Suspending += OnSuspending;
}
public async void Connect()
{
await tcpClient.ConnectAsync(
new Windows.Networking.HostName(Server),
Port.ToString(),
SocketProtectionLevel.PlainSocket);
DataReader reader = new DataReader(tcpClient.InputStream);
Byte[] byteArray = new Byte[1000];
//reader.InputStreamOptions = InputStreamOptions.Partial;
while (true)
{
await reader.LoadAsync(1000);
reader.ReadBytes(byteArray);
// unsafe
//{
// fixed(Byte *fixedByteBuffer = &byteArray[0])
// {
vCtrl.Consume(byteArray);
vCtrl.Decode();
// }
//}
}
}
InputStreamOptions.Partial
意味着当可用字节数少于请求的字节数时,LoadAsync
可能会完成。因此,您不一定能读取所请求的全部缓冲区大小。
试试这个:
public async void Connect()
{
await tcpClient.ConnectAsync(
new Windows.Networking.HostName(Server),
Port.ToString(),
SocketProtectionLevel.PlainSocket);
DataReader reader = new DataReader(tcpClient.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
while (true)
{
var bytesAvailable = await reader.LoadAsync(1000);
var byteArray = new byte[bytesAvailable];
reader.ReadBytes(byteArray);
// unsafe
//{
// fixed(Byte *fixedByteBuffer = &byteArray[0])
// {
vCtrl.Consume(byteArray);
vCtrl.Decode();
// }
//}
}
}
顺便说一句,报告微软漏洞的合适地方是微软连接。