Windows.Networking.Socket - How to get read/write events? RRS feed

  • Question

  • This is regarding Windows::Networking::Socket namespace. I see in SimpleSocket sample how we used SocketStream, InputStream, and OutputStream, etc.

    In my legacy app, where I use WIN32 socket, I detect read/write events of a connected socket and dispatch the event to corresponding event listeners. In particular, I use this Win32 api:

    DWORD WSAWaitForMultipleEvents(
      __in  DWORD cEvents,
      __in  const WSAEVENT *lphEvents,
      __in  BOOL fWaitAll,
      __in  DWORD dwTimeout,
      __in  BOOL fAlertable

    Now in Metro App I do not have this api. But I want somehow detect the read and write events on the SocketStream. How?

    SocketStreamListener seemingly gives ConnectionReceived event.  Is there any event somewhere something like DataRead/DataWritten event, using which I would know my socket has read or written some data, and then I can tell my corresponding event-listeners what to do.

    Please advise.

    • Edited by Shafiq2012 Friday, May 4, 2012 3:47 PM
    Friday, May 4, 2012 3:44 PM

All replies

  • Hello,

    For data read event, you can add the function to DataReader.LoadAsync.Then();

    For data write event, you can add the function to

    Best regards,

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 7, 2012 10:37 AM
  • hi

    you can use DataWriterStoreOperation^ or DataReaderLoadOperation^ to control DataWriter.StoreAsync() or DataReader.LoadAsync() completed status.

    this is a sample about DataWriter.StoreAsync() .you can see it.

    StreamSocket^ socket = ref new StreamSocket(); IOutputStream^ senddata = socket->OutputStream; DatDataWriter^ writer_ = ref new DataWriter(senddata); auto data = ref new Platform::Array<unsigned char>(size); ::memcpy(data->Data, buf, size);//buf is char* style ( your data ) auto task1 = make_task([=] { try { DataWriterStoreOperation^ store_data = nullptr; store_data = writer_ >StoreAsync(); AsyncStatus storestarus = store_data->Status; while (storestarus != AsyncStatus::Completed) { WaitForSingleObjectEx(event_wait,2,false);//event control wait time 2 = 2ms storestarus = store_data->Status;

    // here you should set wait time out to protect it can't return and blocked return; } } catch (Exception^ e) { return; } }); structured_task_group tasks; tasks.run_and_wait(task1);

    • Edited by oishixixi Monday, May 14, 2012 9:44 AM
    Wednesday, May 9, 2012 5:52 AM
  • Hi,

    Thanks for the note.  Where does socket->smi_event_wait come from. I do not see a smi_event_wait property of StreamSocket or of DataWriterStoreOperation.


    Friday, May 11, 2012 6:12 PM
  • Hi,socket->smi_event_wait is a handle of an event,

    you should use createeventexw to create an event and open event where you want use waitforsingleobjectex to wait time

    Monday, May 14, 2012 9:46 AM
  • Are you talking about SocketStream or DatagramStream?  They do not have smi_event_wait.

    If I were to use CreateEventEx(), how exactly will I detect a read or write event of the socket.

    I am looking for similar to WsaEvent().

    Please elaborate what you meant above. I might have misunderstood.


    Thursday, May 17, 2012 11:34 PM
  • you purpose is get event of socket has read or written some data,

    In metro ,this is implemented by DataWriterStoreOperation^ operationstatus or DataReaderLoadOperation^ operationstatus to detect socket has read or written some data.

    because the function is async, so if we want to get status of function executed, wo should wait some times to make ture the function had enough time to execute.

     i use WaitForSingleObjectEx(event_wait,2,false);//event control wait time 2 = 2ms,

    and i will check the status of function exectued once 2ms to make ture the function completed and i knew immediately.

    Friday, May 18, 2012 2:24 AM
  • What if a large data (64K) was sent and the LoadOperation needs 20ms to complete. Then waiting for single object for 2ms will result in time-out and thus end. Then how will I know when the actual operation is over, unless that WaitForSingleEx is under a continuously polling thread. That's CPU expensive.

    Saturday, May 19, 2012 4:06 AM
  • This is a classic issue with using sockets.

    I commonly do lot's of little reads and try to decode the data, but it is important to formulate data with some sort of wrapper, size, or other logically identifiable "packet"


    Saturday, May 19, 2012 10:25 PM
  • Hi Shafiq

    i did test for it ,you can WaitForSingleObjectEx(event_wait,1,false);//event control wait time 2 = 2ms

    and the time is enough to wait for operation::status == completed, the speed of loaddata from Apache server is ahout 6MB/S

    and you don't worry about the preformance.

    use  WaitForSingleObjectEx(event_wait,1,false); in while() it make the data received or sent . if you want to set time out ,you can let return error when while() spent lot of time.

    Monday, May 21, 2012 9:45 AM