在dekstop应用程序c#中使用库Windows.Networking.Sockets(metro应用程序)

本文关键字:应用程序 Sockets Networking metro Windows dekstop | 更新日期: 2023-09-27 18:23:39

最近我开发了一个运行良好的c#metro应用程序,现在我想在c#中开发同样的应用程序。因此,由于这个网站,我几乎没有做什么事情来启用metro应用程序库-库windows.networking.proximity仍在工作,windows.security.cryptography也在工作,但显然windows.netwworking.sockets不工作。我代码这一部分的目标只是接收通过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);
            }
        }
    }


}

}

`当我构建这个代码时,没有抛出任何错误,当我使用wireshark查看从我的smarphone发送的数据包时,我只收到了带有[SYN]标志的数据包,而不是当我使用metro应用程序[SYN]/[SYN,ACK]/[ACK]进行握手时收到的第一个3个数据包。有人知道为什么会发生这种事吗?

在dekstop应用程序c#中使用库Windows.Networking.Sockets(metro应用程序)

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

您添加了吗

<Capability Name="privateNetworkClientServer" />

到Package.appxmanifest?