0

Recently i've developped a c# 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?

4

2 回答 2

1

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

于 2013-08-19T16:21:41.487 回答
0

你加了吗

<Capability Name="privateNetworkClientServer" />

到 Package.appxmanifest?

于 2013-07-19T11:18:41.187 回答