M3579
M3579

Reputation: 910

C# Universal Windows StreamSocket clients not connecting to StreamSocketListener server

I am trying to use C# and UWP to write a simple socket server. So far, I have written a SocketServer class in C# that you can subclass; I want it to be like a socket server in Node.js.

However, I have tried connecting to this server from the Python REPL and from a C# client but both of the times the connection timed out. I compared my code with the StreamSocket example from Microsoft on Github, and I cannot figure out what I am doing wrong. I do not know if it is a problem with my code or with some network configurations on my computer.

Here is my server superclass:

...    
    abstract class SocketServer
    {
        public SocketServer()
        {

        }

        public async void Listen(String port)
        {
            StreamSocketListener serverSocket = new StreamSocketListener();
            await serverSocket.BindServiceNameAsync("8003");

            serverSocket.ConnectionReceived += ClientConnectionReceived;

            Debug.WriteLine("Server started");
        }

        private void ClientConnectionReceived(
            StreamSocketListener sender,
            StreamSocketListenerConnectionReceivedEventArgs args)
        {
            Debug.WriteLine("Client connection received");

            OnConnection(args.Socket);

            DataReader reader = new DataReader(args.Socket.InputStream);

            try
            {
                // fill in later
            }
            catch
            {
                // fill in later
            }
        }

        abstract protected void OnConnection(StreamSocket socket);

        abstract protected void OnData(String data);

        abstract protected void OnEnd(); 
    }
}

Here is the subclass of that server:

class Subclass: SocketServer
    {
    override protected void OnConnection(StreamSocket socket)
    {
        Debug.WriteLine("Got connection");
    }

    override protected void OnData(String data)
    {

    }

    override protected void OnEnd()
    {

    }
}

Here is the code that instantiates the server (in App.xaml.cs):

        public App()
        {
            ...
            Debug.WriteLine("==========================");

            Subclass manager = new Subclass();
            manager.Listen("8003");
        }

And finally, here is the C# client code (in App.xaml.cs):

public App()
        {
            ...
            Debug.WriteLine("========================");

            StartClient();
        }

        async void StartClient()
        {
            try
            {
                Debug.WriteLine("About to connect");
                StreamSocket socket = new StreamSocket();
                Debug.WriteLine("Made StreamSocket");
                await socket.ConnectAsync(new HostName("localhost"), "8003");
                Debug.WriteLine("Connected");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Exit();
            }
        }

It prints "About to connect" and "Made SocketStream", but after a while there is a timeout error.

What am I doing wrong?

Upvotes: 2

Views: 3697

Answers (1)

Jan Skála
Jan Skála

Reputation: 415

Are you connecting from another machine ? You cannot connect to a StreamSocketListener from another app or process running in the same computer, not even with a loopback exemption. You will need to run the client in a different machine.

Upvotes: 6

Related Questions