locked
unable to read bytes[] from socketstream RRS feed

  • Question

  • I am unable to read byte[] using DataReader on Socket.InputStream. Also is it possible to  play video in MediaElement from background thread. I am basically capturing bytes from webcam and want to send it using socketstream. Below is my code:

        //streamSocketListener.ConnectionReceived +=receiveData;
       
    private async void receiveData(StreamSocketListener socketListener, StreamSocketListenerConnectionReceivedEventArgs args)
       
    {
           
    DataReader dataReader = new DataReader(args.Socket.InputStream);
            dataReader
    .InputStreamOptions = InputStreamOptions.Partial;

           
    try
           
    {

                    await dataReader
    .LoadAsync(1024);

                   
    byte[] streamBytes = new byte[dataReader.UnconsumedBufferLength];
                    dataReader
    .ReadBytes(streamBytes);

                    dataReader
    .DetachStream();

                   
    //code to read bytes into receiverrandomaccessstream

                   
    var receiverRandomAccessStream = new InMemoryRandomAccessStream();
                   
    DataWriter dataWriter = new DataWriter(receiverRandomAccessStream);
                    dataWriter
    .WriteBytes(streamBytes);
                    await dataWriter
    .StoreAsync();


                    receiverRandomAccessStream
    .Seek(0);


                   
    //update UI


                    await
    Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        mediaElement
    .SetSource(receiverRandomAccessStream, "video/x-ms-wmv"));


           
    }
           
    catch (Exception exception)
           
    {
               
    Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                       
    ReceiverTextBox.Text = exception.ToString());

           
    }
       
    }

    And the code to send bytes[]

    private async Task sendData(byte[] byte_data)
        {
            Int32 len = 0;
    
            try
            {                
                DataWriter dataWriter = new DataWriter(streamSocket.OutputStream);
    
                dataWriter.WriteUInt32((uint)byte_data.Length);
    
                dataWriter.WriteBytes(byte_data);
    
    
                await dataWriter.StoreAsync();
                await dataWriter.FlushAsync();
    
                dataWriter.DetachStream();
    
                dataWriter.Dispose();
    
            }
            catch (Exception exception)
            {
    
            }
        }


    Monday, October 7, 2013 3:13 PM

All replies

  • The code looks okay as far as sending/receiving goes. Can you capture a Network trace using Microsoft Network Monitor and check whether the data is actually received by the receiver?

    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    Monday, October 7, 2013 11:36 PM
    Moderator
  • I am actually not able to receive. I think there is some problem with byte receive thing. I am getting the following exception:

    System.Exception: The operation identifier is not valid.

       at Windows.Storage.Streams.DataReader.LoadAsync(UInt32 count)

    Also I want to know whether its possible to play video on MediaElement using byte[] received from the background thread.

    Tuesday, October 8, 2013 12:46 AM