http://www.csdn.net/develop/Read_Article.asp?id=25619
VC6实现的!~讲的很详细!~

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Collections.Specialized;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;namespace multicast
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class ChatForm : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private bool done = true;
    private UdpClient client;
    private IPAddress groupAddress;
    private int localPort;
    private int remotePort;
    private int ttl; private IPEndPoint remoteEP;
    private UnicodeEncoding encoding = new UnicodeEncoding();
    private string name;
    private System.Windows.Forms.TextBox textName;
    private System.Windows.Forms.Button sendbtn;
    private System.Windows.Forms.Button startbtn;
    private System.Windows.Forms.Button stopbtn;
    private System.Windows.Forms.TextBox msgbox;
    private System.Windows.Forms.TextBox msgs;
    private System.Windows.Forms.StatusBar statusBar1;
    private string message; public ChatForm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); groupAddress = IPAddress.Parse("234.5.6.11");
    localPort = 7777;
    remotePort = 7777;
    ttl = 32;
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textName = new System.Windows.Forms.TextBox();
    this.sendbtn = new System.Windows.Forms.Button();
    this.startbtn = new System.Windows.Forms.Button();
    this.stopbtn = new System.Windows.Forms.Button();
    this.msgbox = new System.Windows.Forms.TextBox();
    this.msgs = new System.Windows.Forms.TextBox();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.SuspendLayout();
    // 
    // textName
    // 
    this.textName.Location = new System.Drawing.Point(152, 24);
    this.textName.Name = "textName";
    this.textName.Size = new System.Drawing.Size(176, 21);
    this.textName.TabIndex = 0;
    this.textName.Text = "";
    // 
    // sendbtn
    // 
    this.sendbtn.Location = new System.Drawing.Point(392, 24);
    this.sendbtn.Name = "sendbtn";
    this.sendbtn.TabIndex = 1;
    this.sendbtn.Text = "send";
    this.sendbtn.Click += new System.EventHandler(this.sendbtn_Click);
    // 
    // startbtn
    // 
    this.startbtn.Location = new System.Drawing.Point(32, 72);
    this.startbtn.Name = "startbtn";
    this.startbtn.TabIndex = 2;
    this.startbtn.Text = "start";
    this.startbtn.Click += new System.EventHandler(this.startbtn_Click);
    // 
    // stopbtn
    // 
    this.stopbtn.Location = new System.Drawing.Point(152, 72);
    this.stopbtn.Name = "stopbtn";
    this.stopbtn.TabIndex = 3;
    this.stopbtn.Text = "stop";
    this.stopbtn.Click += new System.EventHandler(this.stopbtn_Click);
    // 
    // msgbox
    // 
    this.msgbox.Location = new System.Drawing.Point(272, 72);
    this.msgbox.Multiline = true;
    this.msgbox.Name = "msgbox";
    this.msgbox.Size = new System.Drawing.Size(232, 48);
    this.msgbox.TabIndex = 4;
    this.msgbox.Text = "";
    // 
    // msgs
    // 
    this.msgs.Location = new System.Drawing.Point(32, 136);
    this.msgs.Multiline = true;
    this.msgs.Name = "msgs";
    this.msgs.Size = new System.Drawing.Size(464, 152);
    this.msgs.TabIndex = 5;
    this.msgs.Text = "";
    // 
    // statusBar1
    // 
    this.statusBar1.Location = new System.Drawing.Point(0, 287);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Size = new System.Drawing.Size(536, 22);
    this.statusBar1.TabIndex = 6;
    this.statusBar1.Text = "statusBar1";
    // 
    // ChatForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(536, 309);
    this.Controls.Add(this.statusBar1);
    this.Controls.Add(this.msgs);
    this.Controls.Add(this.msgbox);
    this.Controls.Add(this.stopbtn);
    this.Controls.Add(this.startbtn);
    this.Controls.Add(this.sendbtn);
    this.Controls.Add(this.textName);
    this.Name = "ChatForm";
    this.Text = "Form1";
    this.Closing += new System.ComponentModel.CancelEventHandler(this.ChatForm_Closing);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new ChatForm());
    } private void startbtn_Click(object sender, System.EventArgs e)
    {
    name = textName.Text;
    textName.ReadOnly = true;
    try
    {
    client = new UdpClient(localPort);
    client.JoinMulticastGroup(groupAddress,ttl); remoteEP = new IPEndPoint(groupAddress,remotePort); Thread receiver = new Thread(new ThreadStart(Listener));
    receiver.IsBackground = true;
    receiver.Start(); byte[] data = encoding.GetBytes(name + "has joined the chat");
    client.Send(data,data.Length,remoteEP); startbtn.Enabled = false;
    stopbtn.Enabled = true;
    sendbtn.Enabled = true;
    }
    catch(SocketException ex)
    {
    MessageBox.Show(ex.Message);
    }
    } private void Listener()
    {
    done = false; try
    {
    while(!done)
    {
    IPEndPoint ep = null; byte[] buffer = client.Receive(ref ep);
    message = encoding.GetString(buffer); this.Invoke(new MethodInvoker(DisplayReceivedMessage)); }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    } private void DisplayReceivedMessage()
    {
    string time = DateTime.Now.ToString("t");
    msgs.Text = time + " " + message + "\r\n" + msgs.Text;
    statusBar1.Text = "Received last message at " + time;
    } private void sendbtn_Click(object sender, System.EventArgs e)
    {
    try
    {
    byte[] data = encoding.GetBytes(name + ":" + msgbox.Text);
    client.Send(data,data.Length,remoteEP);
    msgbox.Clear();
    msgbox.Focus();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    } private void stopbtn_Click(object sender, System.EventArgs e)
    {
    StopListener();
    } private void StopListener()
    {
    byte[] data = encoding.GetBytes(name + "has left the chat");
    client.Send(data,data.Length,remoteEP); client.DropMulticastGroup(groupAddress);
    client.Close();
    done = true;
    startbtn.Enabled = true;
    stopbtn.Enabled = false;
    sendbtn.Enabled = false;
    } private void ChatForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if (!done)
    StopListener();
    }
    }
    }