locked
Can't connect between client and server with WinRT StreamSocket... RRS feed

  • Question

  • I designed two Metro application with Javascript. One is server and the other is client.

    This is the part of server code:

    function startServer() {
            var serverName = document.getElementById("servername").value;
            serverListener = new Windows.Networking.Sockets.StreamSocketListener(serverName);
            serverListener.addEventListener("connectionreceived", connectionReceived, false);
            serverListener.bindServiceNameAsync(serverName).then(serverBinded, error);
        }
    
        function connectionReceived(e) {
            if (serverSocket != null) {
                document.getElementById("ServerInfo").innerText = "Already have a listener; call close to close the listener.";
                return;
            }
            document.getElementById("serverInfo").innerText = "Server: connection accepted.";
            serverSocket = e.socket;
            serverReader = new Windows.Storage.Streams.DataReader(serverSocket.inputStream);
            startServerRead();
        }
    
        function serverBinded() {
            document.getElementById("serverInfo").innerText = "Server: listener creation completed.";
        }
    
        function error(reason) {
            if (!serverClosed)
                document.getElementById("serverInfo").innerText = "Server: " + reason;
        }

    It can create server listener and bind port successfully.

    However, when client connects to the server by calling "connectAsync" API. It returns the following error.

    I also upload a part of client code:

    function openClient() {
            if (clientSocket != null) {
                document.getElementById("clientInfo").innerText = "Already have a client; call close to close the listener and the client.";
                return;
            }
            var hostname = document.getElementById("hostname").value;
            var serviceName = document.getElementById("servername").value;
            var socketHost = new Windows.Networking.HostName(hostname);
            clientSocket = new Windows.Networking.Sockets.StreamSocket();
            document.getElementById("clientInfo").innerText = "Client: connection started.";
            clientSocket.connectAsync(socketHost, serviceName).then(onClientAccept, onConnectError);
        }
    
    function onClientAccept() {
            document.getElementById("clientInfo").innerText = "Client: connection completed.";
            clientConnected = true;
        }
    
        function onConnectError(reason) {
            clientSocket = null;
            if (!clientClosed)
                document.getElementById("clientInfo").innerText = reason;
        }

    Please help to figure out the problems in my code. Thanks a lot!

    Friday, March 16, 2012 9:53 AM

Answers

  • You cannot communicate between two separate Metro style apps or Metro style app to another app on the same machine.  This is by design.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, March 16, 2012 6:09 PM
    Moderator
  • Yes you can use sockets to communicate off the machine to another machine.

    -Jeff


    Jeff Sanders (MSFT)

    • Marked as answer by Jie Bao Monday, April 9, 2012 4:11 PM
    Monday, March 19, 2012 12:36 PM
    Moderator

All replies

  • You cannot communicate between two separate Metro style apps or Metro style app to another app on the same machine.  This is by design.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, March 16, 2012 6:09 PM
    Moderator

  • Thank you for your answer, but can I communicate between two different Metro style apps on different machines?

    • Edited by u2mithrandir Monday, March 19, 2012 1:48 AM mistype
    Monday, March 19, 2012 1:47 AM
  • Yes you can use sockets to communicate off the machine to another machine.

    -Jeff


    Jeff Sanders (MSFT)

    • Marked as answer by Jie Bao Monday, April 9, 2012 4:11 PM
    Monday, March 19, 2012 12:36 PM
    Moderator
  • yes you can between two metro style apps from two different machines, but can not in the same machine
    Wednesday, July 18, 2012 4:59 AM