2

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.
Nimp
  • 441
  • 1
  • 5
  • 15
  • Maybe a dup of: http://stackoverflow.com/questions/32665847/cannot-connect-to-streamsocketlistener/32672632#32672632 – kiewic Nov 25 '15 at 01:17
  • Thanks for the answer ! No cause i'm trying from my phone to my computer or from my computer to my phone, not from the same machine. The link that you provided seems to be for a local use contrary to me and there is a little difference for the binding of the listener where there is no connection specified, i have try and i have no more result... its like if the packet never reach the destination. One more time i m not in the case of a localhost. – Nimp Nov 25 '15 at 05:14
  • Did you enabled the *Local/Private network* capabilities in the AppxManifest? – kiewic Nov 25 '15 at 05:33
  • Yes, Internet (client/server) and Private network capabilities are enabled in the AppxManifest. – Nimp Nov 25 '15 at 10:51
  • 1
    Have you tried to use a Local IP address instead of the Internet IP address? Your problem may related to the Internet IP address as your socket server is not available on Internet. You can get Local IP address by referring to `GetCurrentIpv4Address` method in [this file](https://github.com/ms-iot/samples/blob/c44bf4e0b838c0342abc3ca1c0cd535a59c1aa4d/IoTCoreDefaultApp/IoTCoreDefaultApp/Presenters/NetworkPresenter.cs). – Jay Zuo Nov 25 '15 at 12:15
  • I have tried on my local network with local address and it works ! I can establish connection and send some data between phone/computer with my code, now i need to fix it that it works on Internet Ip adress. I think the socket server is available but the packet dont reach the destination... – Nimp Nov 25 '15 at 13:13
  • Can you simply try `listener.BindServiceNameAsync("5043")` instead of `BindServiceNameAsync("5043", SocketProtectionLevel.PlainSocket, internetConnectionProfile.NetworkAdapter)` ? – kiewic Nov 25 '15 at 16:17
  • @kiewic Thanks for your suggestion I have simplified my code and i still have the same result : works good when i use local ip address and as soon i use internet ip address i can't establish a connection... I have update my post with this line rewritten. – Nimp Nov 25 '15 at 23:08
  • Oh, are you trying to connect to your phone from the Internet? I think for security reasons, mobile operators do not allow direct connections to phones through their networks. You will need a middle server. You could look into Azure Queues. – kiewic Nov 25 '15 at 23:32
  • @kiewic Yes i'm on this case, i don't understand how mobile operators can intermediate in this case ? I would be really surprised if its the reason (and I have an unlimited mobile data plan, i can share my phone connection with a laptop for example and make everything on internet with this laptop), and I would need to find an other solution to communicate directly between windows 10 mobile devices/computers, so the Azure Queues that you suggested would not be an alternative for my goal :( – Nimp Nov 26 '15 at 00:13
  • 1
    I insist that it could be the mobile operator who is blocking the connection. See: http://stackoverflow.com/questions/11085160/is-it-possible-to-tcp-connect-to-an-iphone-listening-on-a-port-on-a-3g-network and http://stackoverflow.com/questions/11039848/could-i-connect-to-my-iphone-using-3g-ip – kiewic Nov 26 '15 at 00:27
  • I have seen the link that you have provided, thank you i didnt know this case, it seems that you can be right I will try to make test with two "normal" internet connections. If I'm now in this case, what would you suggest to establish a connection directly between my 2 terminals please ? (let suppose that both terminals know port/IP to use) Finally, there is a point that i still don't understand : if i try to initialise the connection from my mobile terminal, i still don't receive any connection attempt on computer, but i should receive something and then the connection should fail no ? – Nimp Nov 26 '15 at 08:51
  • When using `StreamSocketListener`, make sure that the connecting app and the listening app are not running in the same machine. If the listening app is running in a device within your home or office local network, make sure to configure your router's NAT rules to redirect traffic on port X to your listening computer. – kiewic Nov 26 '15 at 15:29
  • Not sure if you already tried it but there are THREE possible capabilities(rights) you have to give your app. Local Network( Client & Server ) , Internet ( Client ) , Internet ( Client & Server ) – Senador Feb 27 '17 at 08:42

0 Answers0