现在的位置: 首页 > 综合 > 正文

c#广播示例

2012年10月02日 ⁄ 综合 ⁄ 共 6745字 ⁄ 字号 评论关闭
2007-11-22 16:10

//广播数据包,服务端

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.Label label2;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;

   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // 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.label1 = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label2 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(40, 24);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(152, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "输入广播信息";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(96, 200);
    this.button1.Name = "button1";
    this.button1.TabIndex = 2;
    this.button1.Text = "发送";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // textBox1
    //
    this.textBox1.AutoSize = false;
    this.textBox1.Location = new System.Drawing.Point(32, 56);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(240, 112);
    this.textBox1.TabIndex = 3;
    this.textBox1.Text = "textBox1";
    //
    // label2
    //
    this.label2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
    this.label2.Location = new System.Drawing.Point(24, 232);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(248, 40);
    this.label2.TabIndex = 4;
    this.label2.Text = "本程序由辽宁科技大学玄魂制作,QQ717532978";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }

   private void button1_Click(object sender, System.EventArgs e)
   {
    //只能用UDP协议发送广播,所以ProtocolType设置为UDP
    Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
    //让其自动提供子网中的IP地址
    IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast,8899);
    //设置broadcast值为1,允许套接字发送广播信息
    socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast,1);
    //将发送内容转换为字节数组
    byte[]bytes = System.Text.Encoding.Unicode.GetBytes(this.textBox1.Text);
    //向子网发送信息
    socket.SendTo(bytes,iep);
    socket.Close();

   }
}
}

//客户端
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.Button button2;
   EndPoint ep;
   string receiveData;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;

   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    //AcceptMessage(),在未接收广播信息之前,处于阻塞状态,不会生成form
    AcceptMessage();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }
   //接收信息
   private void AcceptMessage()
   {
    //d定义socket对象
    Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
    IPEndPoint iep = new IPEndPoint(IPAddress.Any,8899);
    socket.Bind(iep);
    ep = (EndPoint)iep;
    byte[]bytes = new byte[1024];
    while(true)
    {
     socket.ReceiveFrom(bytes,ref ep);
     receiveData = System.Text.Encoding.Unicode.GetString(bytes);
     receiveData=receiveData.TrimEnd('\u0000');
     Thread th=new Thread(new ThreadStart(Acc));
     th.Start();
     //th.Abort();
       
    }
    socket.Close();
   }
   private void Acc()
   {
    string message = "来自"+ep.ToString()+"的消息";
    DialogResult result = MessageBox.Show(receiveData,message,MessageBoxButtons.AbortRetryIgnore);
   }

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
   this.label1 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
   this.label1.ForeColor = System.Drawing.Color.Black;
   this.label1.Location = new System.Drawing.Point(0, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(424, 23);
   this.label1.TabIndex = 0;
   this.label1.Text = "您已经选择了不再接收广播信息,请确认";
   //
   // button1
   //
   this.button1.BackColor = System.Drawing.Color.Red;
   this.button1.Location = new System.Drawing.Point(80, 88);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "继续接收";
   //this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.BackColor = System.Drawing.Color.Red;
   this.button2.Location = new System.Drawing.Point(240, 88);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "退出系统";
   //this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.BackColor = System.Drawing.Color.Blue;
   this.ClientSize = new System.Drawing.Size(424, 125);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.Name = "Form1";
   this.Text = "提示";
   this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

  
}
}

抱歉!评论已关闭.