Reading on a StreamSocket in C++


  • I may have seriously misunderstood something, but I simply don't get data from a site when connecting to it with a StreamSocket. I have googled around but cannot find the reason. I had a few attempts, now for the sake of simplicity let's use the 'classical' StreamSocket' sample from Microsoft as a context.

    The sample works fine for me, however demonstrates only reading from a StreamSocketListener.

    My assumption is, that building the DataReader on a StreamSocket, instead of a StreamSocketListener should not be too different when dealing with the reader on top of it.

    I just create one in the ScenarioInput1::StartListener_Click(...) function, together with the listener socket:

    // MySocket
    Windows::Networking::Sockets::StreamSocket^ streamSocket = ref new Windows::Networking::Sockets::StreamSocket();
    CoreApplication::Properties->Insert("MySocket", streamSocket);
    Windows::Networking::HostName^ hostName = ref new Windows::Networking::HostName(ref new Platform::String(L"www.microsoft.com") );  // any site
    Concurrency::task<void> connectTask(streamSocket->ConnectAsync(hostName, ref new Platform::String(L"80")));
    connectTask.then([this](task<void> prevTask){
        rootPage->NotifyUserFromAsyncThread("streamSocket Connected", NotifyType::StatusMessage);
    catch (Exception^ exception){
        rootPage->NotifyUserFromAsyncThread("StreamSocket connection failed: " + exception->Message, NotifyType::ErrorMessage);

    there's nothing special in it yet...

    and use it void ScenarioInput1::OnConnection() (instead of the original listener socket) for creating the reader. (The original listener is used only for triggering OnConnection, no need for the sample's locally streamed data.)

    void ScenarioInput1::OnConnection(StreamSocketListener^ listener, 

    StreamSocketListenerConnectionReceivedEventArgs^ object)
        // DataReader^ reader2 = ref new DataReader(object->Socket->InputStream);  // the original reader
        StreamSocket^ socket = dynamic_cast<StreamSocket^>(CoreApplication::Properties->Lookup("MySocket"));
        DataReader^ reader = ref new DataReader(socket->InputStream);
        reader->InputStreamOptions = InputStreamOptions::Partial;
        // Start a receive loop.
        ReceiveStringLoop(reader, object->Socket);

    ... what happens is that debugger will newer stop in the continuations inside ReceiveStringLoop...

    What am I overlooking? Any help/sample/comment is appreciated.

    • Edited by GeorgeH_73 Tuesday, May 29, 2012 3:55 PM
    Tuesday, May 29, 2012 3:51 PM


  • Hello,

    Not clear your point, why use StreamSocket instead of StreamSocketListener ?

    The StreamSocket class provides support for network communication using a TCP stream socket.

    The StreamSocket object is also used in conjunction with the StreamSocketListener object to listen for TCP connections. The StreamSocket object is returned by the Socket property on the ConnectionReceived event when a StreamSocketListener object receives a TCP connection request.

    The typical order of operations is as follows:

    •   Create the StreamSocket.
    • Get socket control data on a StreamSocketControl object using the Control property and set any properties before calling one of the ConnectAsync methods.
    • Call one of the ConnectAsync methods to establish a connection with the remote endpoint. If an SSL/TLS connection is required immediately, this can be specified using some of the ConnectAsync methods. If an SSL/TSL connection is desired after sending and receiving some initial data, then the UpgradeToSslAsync method can be called later to upgrade the connection to use SSL.
    • Get the OutputStream property to write data to the remote host.
    • Get the InputStream property to read data from the remote host.
    • Read and write data as needed.
    • Call the Close method to abort any pending operations and release all unmanaged resources associated with the StreamSocket object.

    From: http://msdn.microsoft.com/library/windows/apps/BR226882

    Best regards,


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

    • Edited by Jesse Jiang Wednesday, May 30, 2012 8:47 AM
    • Marked as answer by Jesse Jiang Wednesday, June 6, 2012 9:25 AM
    Wednesday, May 30, 2012 8:47 AM