tcpuwpclient-serverhololensstream-socket-client

How can I get Windows.Storage.Streams.IInputStream inputStream length?


I use HoloLens 2 as a client and my unity server on my PC. (More discussion about this: How can I read byte array coming from server in UWP app?) I lost my debug await reader1.LoadAsync(256);. I tried everything to get my stream data but I couldn't. I don't want to const value for the buffer I need the exact stream size for the buffer. I tested this and it works only and only if the buffer size and data stream size is equal. Or you can suggest me other approaches?

// Create the StreamSocket and establish a connection to the server.
        using (var streamSocket = new Windows.Networking.Sockets.StreamSocket())
        {
            // The server hostname that we will be establishing a connection to.
            var hostName = new Windows.Networking.HostName(host);

            // client is trying to connect...

            await streamSocket.ConnectAsync(hostName, port);

            // client connected! 

            // Read data from the server.
            using (Windows.Storage.Streams.IInputStream inputStream = streamSocket.InputStream)
            {
                using (var reader1 = new Windows.Storage.Streams.DataReader(inputStream))
                {
                    reader1.InputStreamOptions = Windows.Storage.Streams.InputStreamOptions.ReadAhead;
                    reader1.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    reader1.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;
                    
              
                    // Should be in stream size !!!
                    await reader1.LoadAsync(256);
                    
                    while (reader1.UnconsumedBufferLength > 0)
                    {
                        var bytes1 = new byte[reader1.UnconsumedBufferLength];
                        reader1.ReadBytes(bytes1);

                        // Handle byte array internally!
                        HandleData(bytes1);

                        await reader1.LoadAsync(256);
                    }
                    reader1.DetachStream();
                }
            }
        }
        // close socket
    }
    catch (Exception ex)
    {
        Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
    }

Solution

  • Hi Nico sorry for the late update. I tried everything but TCP is nearly impossible for HoloLens with UWP app. So I tried UDP and it works perfectly (https://github.com/mbaytas/HoloLensUDP). I hope Microsoft put a TCP example for HoloLens 1 and 2 in near future.