1

So I'm fairly new to programming and decided to try some networking but I'm stuck. I tried to create a simple client and server just to establish a connection. Obviously, it doesnt work, and I've looked around but can't find any new information to help me. So I decided to try here.

Here is what's happening when I run the client and server:

(I'm running the client and server on the same computer)

*Client connects to server (localhost):

-System.Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

*Client connects to server when server is not running:

-System.Exception: No connection could be made because the target machine actively refused it.

*Client connects to localhost at port 80 (which is not the server):

-Connection established

*Client connect to HostName google.com at port 80:

-Connection established

*Client connects to random ip and port:

-System.Exception: No such host is known.

Servercode:

public sealed partial class MainPage : Page
{
    StreamSocketListener _listener = new StreamSocketListener();
    string port = "1800";

    public MainPage()
    {
        this.InitializeComponent();
        listener();
    }
    private async void listener()
    {
        _listener.ConnectionReceived += listenerConnectionReceived;
        await _listener.BindServiceNameAsync(port);
    }
    void listenerConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        tb_server.Text = "Connection received.";
    }
    private void btn1_Click(object sender, RoutedEventArgs e)
    {

    }
}

Clientcode:

public sealed partial class MainPage : Page
{
    StreamSocketListener _listener = new StreamSocketListener();
    string port = "1800";

    public MainPage()
    {
        this.InitializeComponent();
        listener();
    }
    private async void listener()
    {
        _listener.ConnectionReceived += listenerConnectionReceived;
        await _listener.BindServiceNameAsync(port);
    }
    void listenerConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        tb_server.Text = "Connection received.";
    }
    private void btn1_Click(object sender, RoutedEventArgs e)
    {

    }
}

1 Answers1

0

Take a look at the StreamSocket sample.

When debugging an app, Visual Studio automatically creates a loopback exemption to test a connection from a WinRT client (StreamSocket) to a local server.

The exemption does not work the other way, from a local client to a WinRT server (StreamSocketListener). You will need to run the server in a different machine.

Also, if you are using SreamSocketListener you need the internetClientServer capability.

If your server is running in another machine in the local network, you need the privateNetworkClientServer capability.

kiewic
  • 15,852
  • 13
  • 78
  • 101