locked
Client unable to receive data from Server in Javascript WinRT

    Question

  • In my test with the StreamSocket sample (http://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931), I can create a listener at localhost, connect client to the listener, send data  from the client to the listener. Howerver, when I modified the app to send data from the listner to the client, the client always fails to receive the data. Is this a new Rule or a bug? This problem does not happen in apps in C#.

    • Edited by Renjie Huang Monday, March 05, 2012 6:49 PM the text is lost
    Monday, March 05, 2012 6:43 PM

Answers

  • The debugger is my best friend...

    The bug was indeed in your javascript.

    You need to write your data from the server socket and not to the client socket.  Change your code to do this and you can start happily coding again!

    var writer = new Windows.Storage.Streams.DataWriter(socketsSample.serverSocket.outputStream);

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 07, 2012 9:00 PM
    Moderator

All replies

  • Hi Rnejie,

    This sounds like it may be a bug.  Can you post the information you modified in the Sample code to reproduce the issue?

    -Jeff


    Jeff Sanders (MSFT)

    Monday, March 05, 2012 9:16 PM
    Moderator
  • Hi Jeff,

    Thanks for your prompt reply. That is very helpful.

    I paste the code diff and the steps below:

    1. Click "StartStreamSocketListener";

    2. Click "Connect to Listener"

    3. You will see "Server send Hello", while it is supposed to be something like "Client read: ....". This mens that the server send the data, but the client fails to receive it.

    If you put a break point at the line "+            var count = socketsSample.clientReader.readInt32();" in js/connectToListener.js, it will never get hit.

    ===================== Diff ========================

    diff --git a/js/connectToListener.js b/js/connectToListener.js
    index 00a5431..216a62a 100644
    --- a/js/connectToListener.js
    +++ b/js/connectToListener.js
    @@ -32,6 +32,35 @@
         function onClientAccept() {
             socketsSample.displayStatus("Client: connection completed.");
             socketsSample.connected = true;
    +
    +        socketsSample.clientReader = new Windows.Storage.Streams.DataReader(socketsSample.clientSocket.inputStream);
    +        socketsSample.clientReader.loadAsync(4).then(function (sizeBytesRead) {
    +            // Make sure 4 bytes were read.
    +            if (sizeBytesRead !== 4) {
    +                socketsSample.displayStatus("Client: connection lost.");
    +                return;
    +            }
    +            // Read in the 4 bytes count and then read in that many bytes.
    +            var count = socketsSample.clientReader.readInt32();
    +            socketsSample.clientReader.loadAsync(count).then(function (stringBytesRead) {
    +                // Make sure the whole string was read.
    +                if (stringBytesRead !== count) {
    +                    socketsSample.displayStatus("Client: connection lost.");
    +                    return;
    +                }
    +                // Read in the string.
    +                var string = socketsSample.clientReader.readString(count);
    +                socketsSample.displayOutput("Client read: " + string);
    +            }, onError); // End of "read in rest of string" function.
    +        }, onError);
    +
    +    }
    +    function onError(reason) {
    +        // When we close a socket, outstanding async operations will be canceled and the
    +        // error callbacks called.  There's no point in displaying those errors.
    +        if (!socketsSample.closing) {
    +            socketsSample.displayStatus(reason);
    +        }
         }
     
         function onConnectError(reason) {
    diff --git a/js/socketsSample.js b/js/socketsSample.js
    index 6850d9f..d94739d 100644
    --- a/js/socketsSample.js
    +++ b/js/socketsSample.js
    @@ -4,6 +4,7 @@
         socketsSample.listener = null; // A StreamSocketListener that acts as our server.
         socketsSample.serverSocket = null; // The server socket that's been accepted.
         socketsSample.serverReader = null; // The reader for the server socket.
    +    socketsSample.serverWriter = null; // The reader for the server socket.
         socketsSample.clientSocket = null; // The client socket that will connect to the server socket.
         socketsSample.connected = false;
         socketsSample.closing = false;
    diff --git a/js/startListener.js b/js/startListener.js
    index a428f7f..9d21642 100644
    --- a/js/startListener.js
    +++ b/js/startListener.js
    @@ -45,7 +45,8 @@
         function onServerAccept(eventArgument) {
             socketsSample.displayStatus("Server: connection accepted.");
             socketsSample.serverSocket = eventArgument.socket;
    -        socketsSample.serverReader = new Windows.Storage.Streams.DataReader(socketsSample.serverSocket.inputStream);
    +       // socketsSample.serverReader = new Windows.Storage.Streams.DataReader(socketsSample.serverSocket.inputStream);
    +        socketsSample.serverWriter = new Windows.Storage.Streams.DataWriter(socketsSample.serverSocket.outputStream);
             startServerRead();
         }
     
    @@ -54,26 +55,22 @@
         // We wait for exactly 4 bytes, read in the count value, and then wait for
         // count bytes, and then display them.
         function startServerRead() {
    -        socketsSample.serverReader.loadAsync(4).then(function (sizeBytesRead) {
    -            // Make sure 4 bytes were read.
    -            if (sizeBytesRead !== 4) {
    -                socketsSample.displayStatus("Server: connection lost.");
    -                return;
    -            }
    -            // Read in the 4 bytes count and then read in that many bytes.
    -            var count = socketsSample.serverReader.readInt32();
    -            socketsSample.serverReader.loadAsync(count).then(function (stringBytesRead) {
    -                // Make sure the whole string was read.
    -                if (stringBytesRead !== count) {
    -                    socketsSample.displayStatus("Server: connection lost.");
    -                    return;
    -                }
    -                // Read in the string.
    -                var string = socketsSample.serverReader.readString(count);
    -                socketsSample.displayOutput("Server read: " + string);
    -                // Restart the read for more bytes.
    -                startServerRead();
    -            }, onError); // End of "read in rest of string" function.
    -        }, onError);
    +
    +        if (!socketsSample.connected) {
    +            socketsSample.displayStatus("Client: you must connect the client before using it.");
    +            return;
    +        }
    +        var writer = new Windows.Storage.Streams.DataWriter(socketsSample.clientSocket.outputStream);
    +        var string = "Hello World";
    +        var len = writer.measureString(string); // Gets the UTF-8 string length.
    +        writer.writeInt32(len);
    +        writer.writeString(string);
    +        socketsSample.displayStatus("Server: sending hello.");
    +        writer.storeAsync().then(onStore, onError);
    +        writer.detachStream(); // Detach stream, if not, DataWriter destructor will close it.
    +    }
    +
    +    function onStore() {
    +        socketsSample.displayStatus("Server: sent hello.");
         }
     })();

    Monday, March 05, 2012 11:19 PM
  • Thanks, I will look at this!

    Jeff Sanders (MSFT)

    Tuesday, March 06, 2012 9:04 PM
    Moderator
  • Thanks.

    I also modified the C++ version of the StreamSocket sample (http://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931) to send data from the server to the client, and in my test the client is able to receive the data.

    There must be a bug in the javasrcipt. Could you help provide me a workaround solution for this ASAP since this issuse totally blocks my work. Thanks so much.

    Wednesday, March 07, 2012 1:08 AM
  • The debugger is my best friend...

    The bug was indeed in your javascript.

    You need to write your data from the server socket and not to the client socket.  Change your code to do this and you can start happily coding again!

    var writer = new Windows.Storage.Streams.DataWriter(socketsSample.serverSocket.outputStream);

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 07, 2012 9:00 PM
    Moderator