Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到UdpClient_C# - Fatal编程技术网

C# 找不到UdpClient

C# 找不到UdpClient,c#,C#,我是c语言编程新手,有以下问题。我想制作一个接收Udp广播的程序。在我的程序中,我想使用UdpClient,但得到的消息是找不到名称空间。我的程序中包括System.Net.Sockets。谁能帮我解决这个问题 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Run

我是c语言编程新手,有以下问题。我想制作一个接收Udp广播的程序。在我的程序中,我想使用UdpClient,但得到的消息是找不到名称空间。我的程序中包括System.Net.Sockets。谁能帮我解决这个问题

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Console
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        UdpClient udpClient = new UdpClient(11000);
        public App()
        {
            Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
                Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
                Microsoft.ApplicationInsights.WindowsCollectors.Session);
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用System.Net.Sockets;
使用System.Runtime.InteropServices.WindowsRuntime;
使用系统文本;
使用System.Threading.Tasks;
使用Windows.ApplicationModel;
使用Windows.ApplicationModel.Activation;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
命名空间控制台
{
/// 
///提供特定于应用程序的行为以补充默认应用程序类。
/// 
密封部分类应用程序:应用程序
{
/// 
///初始化singleton应用程序对象。这是编写代码的第一行
///已执行,因此是main()或WinMain()的逻辑等价物。
/// 
UdpClient UdpClient=新的UdpClient(11000);
公共应用程序()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata|
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
这个.Suspending+=OnSuspending;
}
/// 
///当最终用户正常启动应用程序时调用。其他入口点
///将在启动应用程序以打开特定文件时使用。
/// 
///有关启动请求和过程的详细信息。
仅启动受保护的覆盖无效(启动ActivatedEventArgs e)
{
#如果调试
if(System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter=true;
}
#恩迪夫
Frame rootFrame=Window.Current.Content作为Frame;
//当窗口已经有内容时,不要重复应用程序初始化,
//只需确保窗口处于活动状态
if(rootFrame==null)
{
//创建一个框架作为导航上下文并导航到第一页
rootFrame=新框架();
rootFrame.NavigationFailed+=OnNavigationFailed;
如果(例如,PreviousExecutionState==ApplicationExecutionState.Terminated)
{
//TODO:从先前挂起的应用程序加载状态
}
//将框架放置在当前窗口中
Window.Current.Content=rootFrame;
}
if(rootFrame.Content==null)
{
//导航堆栈未还原时,请导航到第一页,
//通过将所需信息作为导航传递来配置新页面
//参数
导航(typeof(MainPage),例如参数);
}
//确保当前窗口处于活动状态
Window.Current.Activate();
}
/// 
///当导航到某个页面失败时调用
/// 
///导航失败的帧
///有关导航失败的详细信息
void OnNavigationFailed(对象发送方,NavigationFailedEventArgs e)
{
抛出新异常(“加载页面失败”+e.SourcePageType.FullName);
}
/// 
///在挂起应用程序执行时调用。应用程序状态已保存
///不知道应用程序是否会随内容一起终止或恢复
///记忆仍然完好无损。
/// 
///挂起请求的源。
///有关暂停请求的详细信息。
暂停时的私有void(对象发送方,SuspendingEventArgs e)
{
var deleral=e.SuspendingOperation.getdeleral();
//TODO:保存应用程序状态并停止任何后台活动
延迟。完成();
}
}
}
下午好,瓦莫:
我刚刚在我的WPF项目中创建了一个类,它编译得很好:
你在做什么样的项目?这是针对.Net Framework 4.5的Visual Studio 2013

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace HalliburtonCallouts.ViewModel
{
    public  class UPPClass
    {
        public UPPClass()
        {
            // This constructor arbitrarily assigns the local port number.
            UdpClient udpClient = new UdpClient(11000);
            try
            {
                udpClient.Connect("www.contoso.com", 11000);

                // Sends a message to the host to which you have connected.
                Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

                udpClient.Send(sendBytes, sendBytes.Length);

                // Sends a message to a different host using optional hostname and port parameters.
                UdpClient udpClientB = new UdpClient();
                udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);

                //IPEndPoint object will allow us to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                string returnData = Encoding.ASCII.GetString(receiveBytes);

                // Uses the IPEndPoint object to determine which of these two hosts responded.
                Console.WriteLine("This is the message you received " +
                                             returnData.ToString());
                Console.WriteLine("This message was sent from " +
                                            RemoteIpEndPoint.Address.ToString() +
                                            " on their port number " +
                                            RemoteIpEndPoint.Port.ToString());

                udpClient.Close();
                udpClientB.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

您好Wamor:您的代码有很多用法和我的系统难以找到的东西:我想这个答案可以帮助您我只是编译了您的代码,同时用四个注释字符“//”注释了其他项目。关键是找到了UDPClient:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
////using System.Windows.ApplicationModel;
////using Windows.ApplicationModel.Activation;
////using Windows.Foundation;
////using Windows.Foundation.Collections;
////using Windows.UI.Xaml;
////using Windows.UI.Xaml.Controls;
////using Windows.UI.Xaml.Controls.Primitives;
////using Windows.UI.Xaml.Data;
////using Windows.UI.Xaml.Input;
////using Windows.UI.Xaml.Media;
////using Windows.UI.Xaml.Navigation;

namespace Console
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        UdpClient udpClient = new UdpClient(11000);
        public App()
        {
            ////Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
            ////    Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
            ////    Microsoft.ApplicationInsights.WindowsCollectors.Session);
            ////this.InitializeComponent();
            ////this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
////        protected override void OnLaunched(LaunchActivatedEventArgs e)
////        {

////#if DEBUG
////            if (System.Diagnostics.Debugger.IsAttached)
////            {
////                this.DebugSettings.EnableFrameRateCounter = true;
////            }
////#endif

////            Frame rootFrame = Window.Current.Content as Frame;

////            // Do not repeat app initialization when the Window already has content,
////            // just ensure that the window is active
////            if (rootFrame == null)
////            {
////                // Create a Frame to act as the navigation context and navigate to the first page
////                rootFrame = new Frame();

////                rootFrame.NavigationFailed += OnNavigationFailed;

////                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
////                {
////                    //TODO: Load state from previously suspended application
////                }

////                // Place the frame in the current Window
////                Window.Current.Content = rootFrame;
////            }

////            if (rootFrame.Content == null)
////            {
////                // When the navigation stack isn't restored navigate to the first page,
////                // configuring the new page by passing required information as a navigation
////                // parameter
////                rootFrame.Navigate(typeof(MainPage), e.Arguments);
////            }
////            // Ensure the current window is active
////            Window.Current.Activate();
////        }

////        /// <summary>
////        /// Invoked when Navigation to a certain page fails
////        /// </summary>
////        /// <param name="sender">The Frame which failed navigation</param>
////        /// <param name="e">Details about the navigation failure</param>
////        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
////        {
////            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
////        }

////        /// <summary>
////        /// Invoked when application execution is being suspended.  Application state is saved
////        /// without knowing whether the application will be terminated or resumed with the contents
////        /// of memory still intact.
////        /// </summary>
////        /// <param name="sender">The source of the suspend request.</param>
////        /// <param name="e">Details about the suspend request.</param>
////        private void OnSuspending(object sender, SuspendingEventArgs e)
////        {
////            var deferral = e.SuspendingOperation.GetDeferral();
////            //TODO: Save application state and stop any background activity
////            deferral.Complete();
////        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用System.Net.Sockets;
使用System.Runtime.InteropServices.WindowsRuntime;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
////使用System.Windows.ApplicationModel;
////使用Windows.ApplicationModel.Activation;
///使用Windows
// /使用窗口。
////使用Windows.UI.Xaml;
////使用Windows.UI.Xaml.Controls;
////使用Windows.UI.Xaml.Controls.Primitives;
////使用Windows.UI.Xaml.Data;
////使用Windows.UI.Xaml.Input;
////使用Windows.UI.Xaml.Media;
////使用Windows.UI.Xaml.Navigation;
命名空间控制台
{
/// 
///提供特定于应用程序的行为以补充默认应用程序类。
/// 
密封部分类应用程序:应用程序
{
/// 
///初始化singleton应用程序对象。这是编写代码的第一行
///已执行,因此是main()或WinMain()的逻辑等价物。
/// 
UDP客户端