本文介绍了Windows 8 Consumer Preview + Visual Studio 11 Developer Preview 中的Socket BUG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 11 Developer Preview 中编写一个应用程序,在应用程序使用 reader.InputStreamOptions = InputStreamOptions.Partial 运行一段时间后我收到此错误;选项集:

I am writing an app in Visual Studio 11 Developer Preview and I get this error after the app has run for a while with the reader.InputStreamOptions = InputStreamOptions.Partial; option set:

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)

如果未设置该选项,套接字可以正常读取流.

The socket can read the stream fine when that option is not set.

以下是代码供参考:

   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 可以在少于请求的可用字节数时完成.所以你不一定能读取完整的请求缓冲区大小.

InputStreamOptions.Partial means that LoadAsync may complete when less than the requested number of bytes are available. So you can't necessarily read the full requested buffer size.

试试这个:

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();
    // }
    //}
  }
}

顺便说一句,报告 Microsoft 错误的正确位置是 Microsoft Connect.

BTW, the proper place to report Microsoft bugs is Microsoft Connect.

这篇关于Windows 8 Consumer Preview + Visual Studio 11 Developer Preview 中的Socket BUG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 16:26