日期:2014-05-19  浏览次数:20853 次

关于组播Multicast得奇怪问题!!!
本人想做一个象飞鸽一样的东西:能看见局域网内使用本程序的客户.想使用组播,但发现一个特别奇怪的现象:两台机器A(server   2003),B(xp),都运行程序,结果B能给A发,A不能给B发.即是B往组中写入数据,A,B均能读出.但A往组中写入数据,B却不能读出.
      更让我疑惑的是A的IPAddressList有3项172.21.37.21,192.168.115.1,
192.168.0.1,B的IPAddressList只有一项172.21.37.98.
      测试程序中发现,A居然使用第二个IP   192.168.115.1往组播写,B使用它唯一的IP!
      还有就是,A上的组播地址只能为224.0.0.1,否则收不到任何数据!
B机器一切正常!
    测试程序如下:
    using   System;
using   System.Drawing;
using   System.Net;
using   System.Net.Sockets;
using   System.Text;
using   System.Threading;
using   System.Windows.Forms;

public   class   MulticastChat   :   Form
{
TextBox   newText;
ListBox   results;
Socket   sock;
Thread   receiver;
IPEndPoint   multiep   =   new   IPEndPoint(IPAddress.Parse( "224.0.0.1 "),   9050);

public   MulticastChat()
{
Text   =   "Multicast   Chat   Program ";
Size   =   new   Size(400,   380);

Label   label1   =   new   Label();
label1.Parent   =   this;
label1.Text   =   "Enter   text   string: ";
label1.AutoSize   =   true;
label1.Location   =   new   Point(10,   30);

newText   =   new   TextBox();
newText.Parent   =   this;
newText.Size   =   new   Size(200,   2   *   Font.Height);
newText.Location   =   new   Point(10,   55);

results   =   new   ListBox();
results.Parent   =   this;
results.Location   =   new   Point(10,   85);
results.Size   =   new   Size(360,   18   *   Font.Height);

Button   sendit   =   new   Button();
sendit.Parent   =   this;
sendit.Text   =   "Send ";
sendit.Location   =   new   Point(220,52);
sendit.Size   =   new   Size(5   *   Font.Height,   2   *   Font.Height);
sendit.Click   +=   new   EventHandler(ButtonSendOnClick);

Button   closeit   =   new   Button();
closeit.Parent   =   this;
closeit.Text   =   "Close ";
closeit.Location   =   new   Point(290,   52);
closeit.Size   =   new   Size(5   *   Font.Height,   2   *   Font.Height);
closeit.Click   +=   new   EventHandler(ButtonCloseOnClick);

sock   =   new   Socket(AddressFamily.InterNetwork,   SocketType.Dgram,   ProtocolType.Udp);
IPEndPoint   iep   =   new   IPEndPoint(IPAddress.Any,   9050);
sock.Bind(iep);
sock.SetSocketOption(SocketOptionLevel.IP,   SocketOptionName.AddMembership,
new   MulticastOption(IPAddress.Parse( "224.0.0.1 ")));
sock.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive,6);
receiver   =   new   Thread(new   ThreadStart(packetReceive));
receiver.IsBackground   =   true;
receiver.Start();
}

void   ButtonSendOnClick(object   obj,   EventArgs   ea)
{
byte[]   message   =   Encoding.ASCII.GetBytes(newText.Text);
newText.Clear();
sock.SendTo(message,   SocketFlags.None,   multiep);