none
how to get data from streamsocket inputstream

    질문

  • 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





    • 편집됨 oishixixi 2012년 3월 13일 화요일 오전 9:48
    2012년 3월 13일 화요일 오전 6:15

답변

모든 응답

  • 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.

    2012년 3월 13일 화요일 오전 7:59
    중재자
  • 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.

    2012년 3월 13일 화요일 오전 8:32
    중재자
  • 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();

    2012년 3월 13일 화요일 오전 9: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


    • 편집됨 oishixixi 2012년 3월 13일 화요일 오전 9:41
    2012년 3월 13일 화요일 오전 9: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 ?


    • 편집됨 oishixixi 2012년 3월 14일 수요일 오전 2:40
    2012년 3월 14일 수요일 오전 2: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.

    2012년 3월 15일 목요일 오전 3:46
    중재자
  • 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)	
            {        	
            }
         });


    • 편집됨 oishixixi 2012년 3월 15일 목요일 오전 5:53
    2012년 3월 15일 목요일 오전 5: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

    2012년 3월 23일 금요일 오전 3:34
    • 답변으로 표시됨 oishixixi 2012년 3월 28일 수요일 오전 9:26
    2012년 3월 28일 수요일 오전 9:26