Kevin Brant
Kevin Brant

Reputation: 141

Code running in console application but not windows universal app

I have this code in a console app:

using System.Text;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = ASCIIEncoding.ASCII.GetBytes("test message");
            string IP = "192.168.1.22";
            int Port = 2390;
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(IP), Port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.RemoteEndPoint = endPoint;
            socketEventArg.SetBuffer(data, 0, data.Length);
            client.SendToAsync(socketEventArg);
            Debug.WriteLine("sent");
        }
    }
}

and this code in the Windows Universal App:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text;

namespace App6
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            byte[] data = ASCIIEncoding.ASCII.GetBytes("test message");
            string IP = "192.168.1.22";
            int Port = 2390;
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(IP), Port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.RemoteEndPoint = endPoint;
            socketEventArg.SetBuffer(data, 0, data.Length);
            client.SendToAsync(socketEventArg);
            Debug.WriteLine("sent");
        }
    }
}

As you can see the content of both is nearly identical.

Any suggestions or ideas would be helpful!

Upvotes: 0

Views: 310

Answers (1)

IanJ
IanJ

Reputation: 484

Just a guess - have you added the internetClient capability to your app? https://msdn.microsoft.com/en-us/library/windows/apps/mt280233.aspx

Upvotes: 0

Related Questions