On 09.05.2010 17:58, Steve Ricketts wrote:

Alan, majorly lost on your UDP receiver example.  It doesn't look anything
like what I was trying... which is probably a good thing given my approach
hasn't worked at all.  I don't get where the ISearcher or UpnpSearcher are
defined.  Sorry, but I'm totally lost here.

I'm attaching a basic example.

Receiver:

        mono udpreceiver.exe 0.0.0.0 8756

Sender, if the receiver is on local host:

        mono udpsender.exe 127.0.0.1 8756

Sender, if the receiver is somewhere else:

        mono udpsender.exe x.x.x.x 8756

Robert
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Program
{
        static void Main (string[] args)
        {
                if (args.Length < 2) {
                        Console.WriteLine ("usage: udpreceiver <IP-address> 
<port>");
                        return;
                }

                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse 
(args[0]),
                                                           Convert.ToInt32 
(args[1]));
                
                var r = new Receiver (localEndPoint);
                for (;;)
                        Console.WriteLine ("Received: {0}", r.Receive ());

        }
}

class Receiver
{
        Socket sock;
        
        public Receiver(IPEndPoint endPoint)
        {
                sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, 
ProtocolType.Udp);
                sock.Bind (endPoint);
        }

        public string Receive()
        {
                EndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = new byte[1024];
                int count = sock.ReceiveFrom(data, ref clientEndPoint);
                return Encoding.UTF8.GetString(data, 0, count);
        }
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Program
{
        static void Main (string[] args)
        {
                if (args.Length < 2) {
                        Console.WriteLine ("usage: udpsender <IP-address> 
<port>");
                        return;
                }

                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse 
(args[0]),
                                                           Convert.ToInt32 
(args[1]));

                var s = new Sender(remoteEndPoint);
                s.Send("Hello World!");
        }
}

class Sender
{
        Socket sock;
        IPEndPoint remoteEndPoint;
        
        public Sender(IPEndPoint remoteEndPoint)
        {
                sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, 
ProtocolType.Udp);
                this.remoteEndPoint = remoteEndPoint;
        }

        public void Send(string text)
        {
                byte[] data = Encoding.UTF8.GetBytes(text);
                sock.SendTo(data, remoteEndPoint);
        }
}
_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to