Can't establish connection using StreamSocket (universal windows)

柔情痞子 提交于 2019-12-12 18:27:40

问题


Im trying to follow this code sample from microsoft, who is a basic code for sending/receiving data over network from windows 10 computer/phone. Im on VS2015, i have a phone on W10 and my computer also.

The problem is that my application seems to create packet and send one to establish the connection (i have seen this packet with wireshark), but i never received it on the server side.

Here is code to listen port from the actual internet connection available and wait for a connection :

    public static async void StartServer()
    {
        try
        {
            StreamSocketListener listener = new StreamSocketListener();

            //ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            //await listener.BindServiceNameAsync("5043", SocketProtectionLevel.PlainSocket, internetConnectionProfile.NetworkAdapter);

            listener.ConnectionReceived += OnConnection;
            await listener.BindServiceNameAsync("5043");

            Debug.WriteLine("Server Started !");
        }
        catch (Exception)
        {
            Debug.WriteLine("Error StartServer Method !");
        }
    }

The method "OnConnection" is never reach cause the event "ConnectionReceived" is never called.

Here is the code to establish connection (the string ipDestination contain the internet ip address from my phone for example, that i get from checkip.dyndns.org) :

    private static StreamSocket socket;

    public static async void Connect(string ipDestination)
    {
        try
        {
            //Destination Ip address 
            HostName host = new HostName(ipDestination);
            ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

            socket = new StreamSocket();
            socket.Control.KeepAlive = true;

            await socket.ConnectAsync(host, "5043");
            //EXCEPTION RAISE HERE after a moment "System.Runtime.InteropServices.COMException, cant join destination.

            Debug.WriteLine("Connected !");
        }
        catch (Exception)
        {
            Debug.WriteLine("Erreur Connect Method !");
        }
    }

I think i should miss something but i dont know why and im block at this part since a long and can't continue my project... I apologize for the bad english I try to make my best :)

Update from comments :

  • As Jay Zuo suggested, i have try to use local address on private network and it works, i can establish connection, send and receive data without problems... So the problem come when i use internet IP address, and i still can't figure why...
  • As Kiewic suggested, i have simplify my code and commented the precedent version.

来源:https://stackoverflow.com/questions/33895024/cant-establish-connection-using-streamsocket-universal-windows

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