那位高手给个 IP 多播 的例子 http://www.csdn.net/develop/Read_Article.asp?id=25619VC6实现的!~讲的很详细!~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); } }} 用dropdownlist 显示后台数据库的值? 帮忙看看下面的代码那里有问题。 vs2005写代码的时候,光标总是跳出来,不知道为什么? ___100分___求助,c# 中如何将网页存为bmp图??(解决后另开帖再加分)______ 这个字段只在ACCESS字段有用???? 怎样直接在WinForm嵌入media player吗? 求助c#下的分词系统和词库,多谢 如何实现PropertyGrid控件属性多国语言化. 求助,datagrid问题 如何制作C#的卸载程序?高分等待,不够再添 菜鸟求教 Together for Vs.net的安装问题?
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();
}
}
}