Windows 8 Consumer Preview + Visual Studio 11开发人员预览版中的套接字BUG

时间:2012-03-14 20:41:11

标签: c# socket.io windows-8

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


   }

1 个答案:

答案 0 :(得分:1)

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

BTW,报告Microsoft错误的适当位置是Microsoft Connect