在学UDP协议的Socket编程 以下是书上提供的例子,但是调试后发现侦听端接受不了发送
端发出的数据,不知道是什么问题,麻烦大家帮忙了...侦听端程序:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace UDPMulticastListener
{
    class Program
    {
        private static readonly IPAddress GroupAddress =
            IPAddress.Parse("224.168.100.2");
        private const int GroupPort = 11000;
               private static void StartListener()
        {
            bool done = false;
            UdpClient listener = new UdpClient(11000);
            IPEndPoint groupEP = new IPEndPoint(GroupAddress, GroupPort);
            try
            {
                listener.JoinMulticastGroup(GroupAddress);
                listener.Connect(groupEP);                while (!done)
                {
                    Console.WriteLine("Waiting for broadcast");
                    byte[] bytes = listener.Receive(ref groupEP);
                    Console.WriteLine("Received broadcast from {0}:\n {1}\n", 
groupEP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length));                }
                listener.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        static void Main(string[] args)
        {
            StartListener();
        }
    }
}
发送端程序:using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace UDPMulticastSender
{
    class Program
    {        private static readonly IPAddress GroupAddress =
                 IPAddress.Parse("224.168.100.2");
        private const int GroupPort = 11000;        public static void Send(String message)
        {
            UdpClient sender = new UdpClient();
            IPEndPoint groupEP = new IPEndPoint(GroupAddress, GroupPort);
            try
            {
                Console.WriteLine("Sending datagram : {0}",message);
                byte[] bytes = Encoding.ASCII.GetBytes(message);
                sender.Send(bytes, bytes.Length, groupEP);
                sender.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        static void Main(string[] args)
        {
            Send("Hello World!");
        }
    }
}