Windows 8 app如何连接到Socket服务器

Sas*_*iel 2 c# sockets windows-8

我使用这个示例socketAsyncEventArgs完成了一个服务器

在visual studio 2010和.net 4.0中.

现在我正在尝试使用StreamSocket从Windows 8应用程序连接到它,但我得到一个"Acces denied"消息.这是客户端代码:

private StreamSocket streamSocket;
    public string Server = "192.168.0.101";
    public int Port = 9900;

    public async void Connect()
    {
        streamSocket = new StreamSocket();
        Connect();
        try
        {
            await streamSocket.ConnectAsync(
                new Windows.Networking.HostName(Server),
                Port.ToString()); // getting Acces Denied here

            DataReader reader = new DataReader(streamSocket.InputStream);
            reader.InputStreamOptions = InputStreamOptions.Partial;
            while (true)
            {
                var bytesAvailable = await reader.LoadAsync(1000);
                var byteArray = new byte[bytesAvailable];
                reader.ReadBytes(byteArray);

            }
        }
        catch (Exception e)
        {
            MessageBox(e.StackTrace);
        }
    }
Run Code Online (Sandbox Code Playgroud)

如何解决问题?是否有另一种使用此服务器发送和接收消息的方法?

Joh*_*ner 12

您可能还会在错误消息中看到以下内容:

WinRT information: A network capability is required to access this network resource
Run Code Online (Sandbox Code Playgroud)

这是因为您需要为应用程序添加一个允许您访问本地网络的功能.双击Package.appxmanifest项目中的文件.单击Capabilities选项卡.将Private Networks (Client & Server)功能添加到项目中.