Sas Gabriel
Sas Gabriel

Reputation: 1556

Windows 8 app how to connect to Socket server

i have done a server using this example socketAsyncEventArgs

in visual studio 2010 and .net 4.0.

Now i'm trying to connect to it from a windows 8 app using StreamSocket but i'm getting a "Acces denied" message. here is the Client code:

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);
        }
    }

How to fix the problem? Is there another way to send and receive messages using this server?

Upvotes: 2

Views: 6220

Answers (1)

John Koerner
John Koerner

Reputation: 38087

You are probably also seeing the following as part of your error message:

WinRT information: A network capability is required to access this network resource

This is because you need to add a capability to your application that allows you to access local networks. Double click on the Package.appxmanifest file in your project. Click on the Capabilities tab. Add the Private Networks (Client & Server) capability to your project.

Upvotes: 12

Related Questions