简体   繁体   English

代码在控制台应用程序中运行,但不在Windows通用应用程序中运行

[英]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: 以及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. 如您所见,两者的内容几乎相同。

  • The IP I set it to send to is a program I'm running on a different computer that I have confirmed works. 我将其设置为发送到的IP是我在另一台已确认可以正常运行的计算机上运行的程序。 When I run the console application, it displays the data I sent properly on the program on the other computer, and the console application prints out "sent" on the debug console as it should. 当我运行控制台应用程序时,它将显示我在另一台计算机上的程序上正确发送的数据,并且控制台应用程序应按需在调试控制台上打印出“已发送”。 The Universal Windows App similarly prints out "sent", however the packet never reaches the other computer. 通用Windows应用程序类似地打印出“已发送”,但是数据包永远不会到达另一台计算机。

  • I checked using WireShark (a network analysis/packet sniffing tool) and it appears that when I use the console application, a UDP packet is sent, but when I use the Universal Windows app, no UDP packets are found. 我使用WireShark(网络分析/数据包嗅探工具)进行了检查,看来当我使用控制台应用程序时,发送了UDP数据包,但是当我使用通用Windows应用程序时,没有找到UDP数据包。

  • I am running both programs from Visual Studio 2015 in debug mode, and I can't think of any reason why it wouldn't be working. 我正在Visual Studio 2015中以调试模式运行这两个程序,我想不出它为什么无法运行的任何原因。

Any suggestions or ideas would be helpful! 任何建议或想法都会有所帮助!

Just a guess - have you added the internetClient capability to your app? 只是一个猜测-您是否已将internetClient功能添加到您的应用程序中? https://msdn.microsoft.com/en-us/library/windows/apps/mt280233.aspx https://msdn.microsoft.com/zh-CN/library/windows/apps/mt280233.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从控制台应用程序连接到Windows Universal App StreamSocket? - How do I connect to a Windows Universal App StreamSocket from a Console Application? 从Windows服务和控制台应用程序访问相同的运行代码? - Access the same running code from a Windows Service and a console application? 代码在控制台应用程序中有效,但在Windows Service中运行时不起作用 - Code works in a console application, but does not work when running in windows service .NET应用程序作为Windows窗体或控制台应用程序运行 - .NET app running as either Windows Form or as Console Application 在Windows 10 Universal App上捕获应用程序的屏幕 - Capture screen of the application on a Windows 10 Universal App 空白通用Windows应用和控制台应用之间的区别 - Difference between blank universal windows app and console app 单击按钮在Windows应用程序上运行控制台应用程序 - Running console application on windows application with button click 通用Windows程序集是否适用于.net控制台应用程序? - Do universal windows assemblies work with a .net console app? 运行 C# 控制台应用程序作为 Windows 服务 - Running a C# console application as a Windows service 在Mac或Windows上运行.NET Core Console应用程序 - Running a .NET Core Console Application on Mac or Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM