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

    질문

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


    • 편집됨 Shafiq2012 2012년 5월 4일 금요일 오후 3:47
    2012년 5월 4일 금요일 오후 3:44

모든 응답

  • Hello,

    For data read event, you can add the function to DataReader.LoadAsync.Then();
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.loadasync.aspx

    For data write event, you can add the function to
    DataWriter.StoreAsync.Then();
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datawriter.storeasync.aspx

    Best regards,
    Jesse


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

    2012년 5월 7일 월요일 오전 10:37
    중재자
  • 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);




    • 편집됨 oishixixi 2012년 5월 14일 월요일 오전 9:44
    2012년 5월 9일 수요일 오전 5:52
  • 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.

    -Shafiq

    2012년 5월 11일 금요일 오후 6:12
  • 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

    2012년 5월 14일 월요일 오전 9:46
  • 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.

    -Shafiq.

    2012년 5월 17일 목요일 오후 11:34
  • 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.

    2012년 5월 18일 금요일 오전 2:24
  • 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.

    2012년 5월 19일 토요일 오전 4:06
  • 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"


    www.puttnutz.com

    2012년 5월 19일 토요일 오후 10:25
  • 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.

    2012년 5월 21일 월요일 오전 9:45