I'm trying to connect to a StreamSocketListener in my Windows 10 app. This is working if the client socket is inside the same app. But if I try to connect from another application (e.g. Putty) it doesn't work. After a few seconds putty says "Network Error: Connection Refused".
Here is my sample code:
public sealed partial class MainPage : Page
{
private StreamSocketListener listener;
public MainPage()
{
this.InitializeComponent();
listener = new StreamSocketListener();
listener.ConnectionReceived += Listener_ConnectionReceived;
listener.BindServiceNameAsync("12345").AsTask().Wait();
}
private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
Debug.WriteLine("new connection");
string message = "Hello World!";
using (var dw = new DataWriter(args.Socket.OutputStream))
{
dw.WriteString(message);
await dw.StoreAsync();
dw.DetachStream();
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
// Test connection
var serverHost = new HostName("localhost");
var socket = new StreamSocket();
await socket.ConnectAsync(serverHost, "12345");
using (var dr = new DataReader(socket.InputStream))
{
dr.InputStreamOptions = InputStreamOptions.Partial;
await dr.LoadAsync(12);
var input = dr.ReadString(12);
Debug.WriteLine("received: " + input);
}
}
}
In XAML i added a button to test the client connection.
In the manifest i have checked "Internet (Client)", "Internet (Client & Server)" and "Private Networks (Client & Server)".
EDIT: I'm trying to connect on the same computer. Firewall is deactivated.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With