Socket BUG in Windows 8 Consumer Preview + Visual Studio 11 Developer Preview

3

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.

Here is the code for reference:

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


   }
c#
socket.io
windows-8
asked on Stack Overflow Mar 14, 2012 by pkumar0 • edited Mar 14, 2012 by Edward Thomson

1 Answer

1

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.

Try this:

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, the proper place to report Microsoft bugs is Microsoft Connect.

answered on Stack Overflow Mar 17, 2012 by Stephen Cleary

User contributions licensed under CC BY-SA 3.0