locked
Receiving data from streamsocket using DataReader

    Question

  • In case of DatagramSsocket when a datagram arrives MessageReceived  event is fired and we can receive entire datagram altogether.  So in case of StreamSocket, how can I receive the entire tcp packet altogether??? I would like to receive data in byte streams.

    I tried with following code snapped,

    // Read first 4 bytes (length of the subsequent string). 
        task<unsigned int>(reader->LoadAsync(sizeof(UINT32))).then([this, reader, socket] (unsigned int size) 
        { 
            if (size < sizeof(UINT32)) 
            { 
                // The underlying socket was closed before we were able to read the whole data. 
                cancel_current_task(); 
            } 
     
            unsigned int stringLength = reader->UnconsumedBufferLength; 
            return task<unsigned int>(reader->LoadAsync(stringLength)).then([this, reader, stringLength] (unsigned int actualStringLength) 
            { 
                if (actualStringLength != stringLength) 
                { 
                    // The underlying socket was closed before we were able to read the whole data. 
                    cancel_current_task(); 
                } 
                 
                // Display the string on the screen. This thread is invoked on non-UI thread, so we need to marshal the call back to the UI thread. 
                IBuffer^ receivedStreamBuffer = reader->ReadBuffer(actualStringLength );
    	    String^ dataInHexEncodedString = CryptographicBuffer::EncodeToHexString(receivedStreamBuffer);
                OutputDebugStringW(dataInHexEncodedString->Data());
             }); 
        }).then([this, reader, socket] (task<void> previousTask) 
        { 
            try 
            { 
                // Try getting all exceptions from the continuation chain above this point. 
                previousTask.get(); 
     
                // Everything went ok, so try to receive another string. The receive will continue until the stream is broken (i.e. peer closed closed the socket). 
                ReceiveStringLoop(reader, socket); 
            } 
            catch (Exception^ exception) 
            { 
                NotifyUserFromAsyncThread("Read stream failed with error: " + exception->Message, NotifyType::ErrorMessage); 
      
            } 
            catch (task_canceled&) 
            { 
                // Do not print anything here - this will usually happen because user closed the client socket. 
    
            } 
        }); 

    Here I can not get entire data packet at OutputDebugStringW() at a time, even it break down the packet. I also get some additional data at several times. Can anybody clarify/explain it?

    My purpose is to receive the entire tcp packet immediately from stream line.



    Wednesday, January 9, 2013 1:40 PM

Answers

  • Hi,

    We can change the read size using LoadAsync, In your codes, we load the 4 bytes to get the string length first, and then using loadAsync again to load the whole string.

    If we can know the packet size, we can use loadAsync to load the whole packet.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Monday, January 14, 2013 2:50 AM
    • Marked as answer by Jesse Jiang Tuesday, January 15, 2013 2:10 AM
    Thursday, January 10, 2013 7:15 AM

All replies

  • Hi,

    We can change the read size using LoadAsync, In your codes, we load the 4 bytes to get the string length first, and then using loadAsync again to load the whole string.

    If we can know the packet size, we can use loadAsync to load the whole packet.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Monday, January 14, 2013 2:50 AM
    • Marked as answer by Jesse Jiang Tuesday, January 15, 2013 2:10 AM
    Thursday, January 10, 2013 7:15 AM
  • What if you don't know the size of the packet in advance??
    Thursday, August 7, 2014 1:12 AM