Question

I'm trying to make a basic client-server application for windows phone 7 (using Mango 7.1). At the moment I just have the sample code from MSDN (here: http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx) for a UDP client pasted into a method in Visual Studio. For some reason, although I have all the right references, I'm being told that "System.New.Sockets.Socket does not contain a definition for 'SendTo' and no extension method 'SendTo' accepting a first argument of type 'System.Net.Sockets.Socket could be found". I've got .NET 4.0 which supposedly supports SendTo as is used in the sample code. Not sure what to do, any help would be appreciated.

Here's the code if it's any use, just keep in mind that I literally pasted it in from the sample code and have yet to put it in context at all:

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");

        }


    }
}
Was it helpful?

Solution

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.

OTHER TIPS

You probably need to add System.Net as a referenced assembly in the project references. A "using" statement just qualifies namespaces in source code, it doesn't have anything to do with the compiler actually finding the referenced code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top