how to get data from streamsocket inputstream
-
13. března 2012 6:15
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
- Upravený oishixixi 13. března 2012 9:48
Všechny reakce
-
13. března 2012 7:59Moderátor
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. března 2012 8:32Moderátor
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. března 2012 9:35
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. března 2012 9:40
i want ask that when i send a request to Apache server
could i receive the response from the streamsocket inpustream immediately ?
::best regards
- Upravený oishixixi 13. března 2012 9:41
-
14. března 2012 2:37
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 ?- Upravený oishixixi 14. března 2012 2:40
-
15. března 2012 3:46Moderátor
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. března 2012 5:45
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) { } });
- Upravený oishixixi 15. března 2012 5:53
-
23. března 2012 3:34
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
-
28. března 2012 9:26
I found root cause.
please reference this
- Označen jako odpověď oishixixi 28. března 2012 9:26