如何从控制台应用程序连接到 Windows 通用应用程序 StreamSocket? [英] How do I connect to a Windows Universal App StreamSocket from a Console Application?

查看:30
本文介绍了如何从控制台应用程序连接到 Windows 通用应用程序 StreamSocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在侦听通过 Windows 通用应用程序的连接,并希望通过 Windows 控制台应用程序连接到该应用程序.我已经完成了一些我认为应该连接的基本代码,但我从控制台应用程序收到超时错误.

I am listening for connections through a Windows Universal App and would like to connect to that App through a Windows Console Application. I have done some basic code which I think should connect but I get a timeout error from the console application.

{"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 192.168.0.5:1771"}

windows 通用应用程序甚至从未进入连接接收功能.

The windows universal app never even goes into the connection received function.

服务器(UWP):

    public async void SetupServer()
    {
        try
        {
            //Create a StreamSocketListener to start listening for TCP connections.
            Windows.Networking.Sockets.StreamSocketListener socketListener = new Windows.Networking.Sockets.StreamSocketListener();

            //Hook up an event handler to call when connections are received.
            socketListener.ConnectionReceived += SocketListener_ConnectionReceived;

            //Get Our IP Address that we will host on.
            IReadOnlyList<HostName> hosts = NetworkInformation.GetHostNames();
            HostName myName = hosts[3];

            //Assign our IP Address
            ipTextBlock.Text = myName.DisplayName+":1771";
            ipTextBlock.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255,0,255,0));

            //Start listening for incoming TCP connections on the specified port. You can specify any port that' s not currently in use.
            await socketListener.BindEndpointAsync(myName, "1771");
        }
        catch (Exception e)
        {
            //Handle exception.
        }
    }

客户端(控制台应用程序):

The Client (Console Application):

static void Main(string[] args)
    {
        try
        {
            byte[] data = new byte[1024];
            int sent;
            string ip = "192.168.0.5";
            int port = 1771;
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port);
            TcpClient client = new TcpClient();
            client.Connect(ipep); //**************Stalls HERE************
            using (NetworkStream ns = client.GetStream())
            {
                using (StreamReader sr = new StreamReader(ns))
                {
                    using (StreamWriter sw = new StreamWriter(ns))
                    {
                        sw.WriteLine("Hello!");
                        sw.Flush();
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine("Response: " + sr.ReadLine());
                    }
                }
            }
        }
        catch (Exception e)
        {

        }
    }

推荐答案

我已经在我这边测试了你的代码,它可以很好地工作.所以你的代码没有任何问题.但是我可以通过在同一设备上运行服务器和客户端来重现您的问题,客户端将抛出与上面显示的相同的异常.所以请确保您是从另一台机器连接的.我们无法连接到 uwp 应用 StreamSocketListener从在同一台机器上运行的另一个应用程序或进程,这是不允许的.甚至没有环回豁免.

I have tested your code on my side and it can work well. So there is nothing wrong with your code. But I can reproduce your issue by running the server and client on the same device, the client will throw the same exception as you showed above. So please ensure you're connecting from another machine. We cannot connect to a uwp app StreamSocketListener from another app or process running on the same machine,this is not allowed. Not even with a loopback exemption.

还请确保 Internet(Client&Server) 功能 已启用.在客户端,您可以成功 ping 服务器 192.168.0.5.

Please also ensure the Internet(Client&Server) capability is enabled. And on the client you can ping the server 192.168.0.5 successfully.

这篇关于如何从控制台应用程序连接到 Windows 通用应用程序 StreamSocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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