无法连接到 StreamSocketListener [英] Cannot connect to StreamSocketListener

查看:25
本文介绍了无法连接到 StreamSocketListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到我的 Windows 10 应用程序中的 StreamSocketListener.如果客户端套接字在同一个应用程序中,这将起作用.但是,如果我尝试从另一个应用程序(例如 Putty)进行连接,则它不起作用.几秒钟后腻子说网络错误:连接被拒绝".

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".

这是我的示例代码:

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);
        }
    }
}

在 XAML 中,我添加了一个按钮来测试客户端连接.

In XAML i added a button to test the client connection.

在清单中,我检查了Internet(客户端)"、Internet(客户端和服务器)"和私有网络(客户端和服务器)".

In the manifest i have checked "Internet (Client)", "Internet (Client & Server)" and "Private Networks (Client & Server)".

我正在尝试在同一台计算机上进行连接.防火墙已停用.

I'm trying to connect on the same computer. Firewall is deactivated.

推荐答案

您无法从在同一台计算机上运行的另一个应用程序或进程连接到 StreamSocketListener,即使使用 环回豁免.您需要在另一台机器上运行客户端.

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.

这篇关于无法连接到 StreamSocketListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆