[英]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.