How do I connect to a Windows Universal App StreamSocket from a Console Application?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 04:32:23

问题


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

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

The Server (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)
        {

        }
    }

回答1:


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.

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



来源:https://stackoverflow.com/questions/40474244/how-do-i-connect-to-a-windows-universal-app-streamsocket-from-a-console-applicat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!