简体   繁体   中英

Use library Windows.Networking.Sockets ( metro app ) in a dekstop app c#

Recently i've developped ac# metro app which works well and now I want to developp the same app in c#. So I've done few things to enable the use of the metro app library thanks to this website the library windows.networking.proximity still working, windows.security.cryptography also but apparently windows.networking.sockets doesn't work. The goal in this part of my code, is just to receive data sent from a smartphone by wifi:

namespace OpenItForMeDesktop{
class Server
{
    private StreamSocketListener serverListener;
    public static String port = "3011";
    public static String adressIP = "192.168.173.1";
    private HostName hostName;

    //Initialize the server
    public Server()
    {
        serverListener = new StreamSocketListener();
        hostName = new HostName(adressIP);
        listen();
    }

    //Create the listener which is waiting for connection
    private async void listen()
    {
        serverListener.ConnectionReceived += OnConnection;
        try
        {

            //await serverListener.BindEndpointAsync(hostName, port);
            await serverListener.BindServiceNameAsync(port);
            MainWindow.Current.UpdateLog("Listening for Connection(s)");
        }
        catch (Exception exception)
        {
            MainWindow.Current.UpdateLog("Exception throw in Listen : " + exception);
        }

    }

    //When a connection appears, this function his called
    private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {

        MainWindow.Current.UpdateLog("A message has been received...");
        if (MainWindow.Current.loadingPage)
        {
            MainWindow.Current.UpdateLog("wait please");
        }
        else
        {

            DataReader reader = new DataReader(args.Socket.InputStream);
            try
            {
                while (true)
                {

                    reader.InputStreamOptions = InputStreamOptions.Partial;
                    // Read first 4 bytes (length of the subsequent string).
                    uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
                    if (sizeFieldCount != sizeof(uint))
                    {
                        return;
                    }

                    // Read the string.
                    uint stringLength = reader.ReadUInt32();
                    uint actualStringLength = await reader.LoadAsync(stringLength);
                    if (stringLength != actualStringLength)
                    {
                        return;
                    }
                    String message = reader.ReadString(actualStringLength);
                    MainWindow.Current.receiveMessage(message);
                }
            }
            catch (Exception exception)
            {
                // If this is an unknown status it means that the error is fatal and retry will likely fail.
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }

                MainWindow.Current.UpdateLog("Read stream failed with error: " + exception.Message);
            }
        }
    }




}

}

` When I build this code there is no error which is thrown, and when I use wireshark to see the packet sent from my smarpthone, I received only packet with the flag [SYN] and not the 3 first packets I received when i'm using my metro app [SYN]/[SYN,ACK]/[ACK] for the handshake. Is someone as any idea why this is happen?

我找到了答案,这很愚蠢,它是Windows防火墙阻止了我的应用程序。

Did you add

<Capability Name="privateNetworkClientServer" />

to the Package.appxmanifest?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM