none
how to get data from streamsocket inputstream

    Întrebare

  • Hi

    I want to get data from inputstream,

    IInputStream^ receivedata = socket->InputStream;     
    auto reader = ref new DataReader(receivedata);
    reader->InputStreamOptions = InputStreamOptions(1);
    task<unsigned int>(reader->LoadAsync(size)).get();
    auto data = ref new Platform::Array<unsigned char>(size);
    task<unsigned int>(reader->ReadBytes(data)).get();
    char* buf = nullptr;
    ::memcpy(buf, data->Data , size);

    i can't find the wrong.

    I find the reason, receive data size is 64kb

    when readdata() it will return Ox8000000b,The operation attempted to access data outside the valid range.

    but I can't resolve this issue

    PS:i'm a rookie





    13 martie 2012 06:15

Toate mesajele

  • Hi,

    In general, you can use DataReader in conjunction with IInputStream to read data from StreamSocket.

    Please refer to StreamSocket sample for more detailes.


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    13 martie 2012 07:59
    Moderator
  • It seems that ReadData method is not defined in DataReader class. What happened behind the ReadData method in your code? 

    You can try to use DataReader.ReadBuffer method or other typed ReadXXX methods. Meanwhile, please make sure the desired reading size is not out of boundary.


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    13 martie 2012 08:32
    Moderator
  • I'm sorry about making a slip in writing ReadData, it's ReadBytes actually.

    I modify it.

    the error happen when running

    task<unsigned int>(reader->ReadBytes(size)).get();

    13 martie 2012 09:35
  • i want ask that when i send a request to Apache server

    could i receive the response from the streamsocket inpustream immediately ?

    ::best regards


    13 martie 2012 09:40
  • Hi,Robin

    I did a test when I modify code like this

    task<unsigned int> loaddata(smi_reader->LoadAsync(size));
    loaddata.then([=](task<unsigned int> loaddata)
        {
            try
            {
                for (int i = 0; i < size; i++)
                {
                    unsigned char iii = smi_reader->ReadByte();
    	       memcpy(buf+i,&iii,1);
    	  }		
            }
            catch (Exception^ exception)	
            {        	
            }
         });
    	

    it can get data,

    but if write

    task<unsigned int>(reader->LoadAsync(size)).get();

    i will be error
    how did i know LoadAsync had be completed ?


    14 martie 2012 02:37
  • task::get method returns the result this task produced. If the task is not in a terminal state, a call to get will wait for the task to finish.

    Please refer to task::get for more details.

    You can attach your event handler to DataReaderLoadOperation.Completed to check when LoadAync completed.

    DataReaderLoadOperation object is returned by DataReader.LoadAsync


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    15 martie 2012 03:46
    Moderator
  • Dear Robin

    if I can't get data from streamsocket inputstream before I use it, when I use the follow code in my APP for receive, it will never get data

    the app will goes  wrong.

    if I use LoadAsync() wrong,please tell me.

    because I use the same code in StreamSocket Sample for test, it's worked.

    add it follow send data, and the test server will receive data,

    task<unsigned int> loaddata(smi_reader->LoadAsync(size));
    loaddata.then([=](task<unsigned int> loaddata)
        {
            try
            {
                for (int i = 0; i < size; i++)
                {
                    unsigned char iii = smi_reader->ReadByte();
    	       memcpy(buf+i,&iii,1);
    	  }		
            }
            catch (Exception^ exception)	
            {        	
            }
         });


    15 martie 2012 05:45
  • I have a question.

    if  I use Metro UI app to test StreamSocket receive data.

    when call LoadAsync() and try to read data from inputstream immediately.

    exception will show result 0x8000000b.

    but if let it go by itself ,the APP will read data after the main() exiting.

    so why about this

    23 martie 2012 03:34