Ошибка Socket в Windows 8 Consumer Preview + Visual Studio 11 Developer Preview - PullRequest
3 голосов
/ 15 марта 2012

Я пишу приложение в Visual Studio 11 Developer Preview, и я получаю эту ошибку после того, как приложение какое-то время запускается, с reader.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 Ответ

1 голос
/ 17 марта 2012

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

Кстати, правильное место для сообщений об ошибках Microsoft: Microsoft Connect .

...