2

我正在尝试为 windows phone 7(使用 Mango 7.1)制作一个基本的客户端服务器应用程序。目前,我只有来自 MSDN(此处:http: //msdn.microsoft.com/en-us/library/tst0kwb1.aspx)的示例代码,用于将 UDP 客户端粘贴到 Visual Studio 中的方法中。出于某种原因,虽然我有所有正确的引用,但我被告知“System.New.Sockets.Socket 不包含'SendTo'的定义,并且没有扩展方法'SendTo'接受'System'类型的第一个参数.Net.Sockets.Socket 可以找到”。我有.NET 4.0,它应该支持示例代码中使用的 SendTo。不知道该怎么做,任何帮助将不胜感激。

如果有任何用处,这里是代码,请记住,我是从示例代码中直接将其粘贴到其中的,而根本没有将其放在上下文中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Net.Sockets;
using System.Text;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {

        var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
            ProtocolType.Udp);

        IPAddress broadcast = IPAddress.Parse("192.168.1.255");

        byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]);
        IPEndPoint ep = new IPEndPoint(broadcast, 11000);

        s.SendTo(sendbuf, ep);



        Console.WriteLine("Message sent to the broadcast address");

        }


    }
}
4

2 回答 2

4

You don't have .NET 4.0 on the phone. You're building a phone app, so you need to stick to the APIs supported on the phone - look at the Silverlight version of System.Net.Sockets and within any type, you're restricted to the members with the phone icon next to them.

In particular synchronous APIs such as Socket.SendTo aren't generally supported on Windows Phone 7 - so you'll need to use Socket.SendToAsync.

于 2012-01-31T17:37:36.993 回答
0

您可能需要将 System.Net 添加为项目引用中的引用程序集。“使用”语句仅限定源代码中的名称空间,它与编译器实际查找引用代码没有任何关系。

于 2012-01-31T21:03:39.890 回答