c#广播示例

简介:
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());
}

  
}
}


本文转自悬魂博客园博客,原文链接:http://www.cnblogs.com/xuanhun/archive/2010/02/03/1662415.html,如需转载请自行联系原作者
相关文章
|
2月前
|
数据安全/隐私保护 C# UED
利用 Xamarin 开展企业级移动应用开发:从用户登录到客户管理,全面演示C#与Xamarin.Forms构建跨平台CRM应用的实战技巧与代码示例
【8月更文挑战第31天】利用 Xamarin 进行企业级移动应用开发能显著提升效率并确保高质量和高性能。Xamarin 的跨平台特性使得开发者可以通过单一的 C# 代码库构建 iOS、Android 和 Windows 应用,帮助企业快速推出产品并保持一致的用户体验。本文通过一个简单的 CRM 示例应用演示 Xamarin 的使用方法,并提供了具体的代码示例。该应用包括用户登录、客户列表显示和添加新客户等功能。此外,还介绍了如何增强应用的安全性、数据持久化、性能优化及可扩展性,从而构建出功能全面且体验良好的移动应用。
35 0
|
2月前
|
机器学习/深度学习 数据挖掘 C#
ONNX Runtime入门示例:在C#中使用ResNet50v2进行图像识别
ONNX Runtime入门示例:在C#中使用ResNet50v2进行图像识别
59 0
|
4月前
|
C#
C#数值类型介绍及示例
C#数值类型介绍及示例
|
5月前
|
监控 安全 C#
开发公司电脑监控软件的报警系统:一个C#示例
在当今数字化时代,企业对其计算机网络和系统的安全性和稳定性越来越重视。为了确保员工遵守公司政策、保护机密信息以及监控系统的正常运行,开发一种可靠的公司电脑监控软件变得至关重要。本文将介绍如何使用C#编写一个简单而有效的报警系统,以便监控关键数据并在必要时发出警报。
152 0
|
5月前
|
C#
C#断点续传的实现示例
C#断点续传的实现示例
62 0
|
5月前
|
定位技术 C# 图形学
Unity和C#游戏编程入门:创建迷宫小球游戏示例
Unity和C#游戏编程入门:创建迷宫小球游戏示例
137 2
|
5月前
|
C#
C# 布尔值和条件语句:入门指南和实用示例
在编程中,通常需要一个只能有两个值之一的数据类型,比如: 是 / 否 开 / 关 真 / 假 为此,C# 有一个 bool 数据类型,可以取 true 或 false 的值。
103 3
|
5月前
|
存储 C#
C# 数据类型与类型转换:包含教程与示例
使用正确的数据类型对应于相应的变量是重要的;这样可以避免错误、节省时间和内存,还会使您的代码更易于维护和阅读。最常见的数据类型有:
50 0
|
12月前
|
定位技术 API C#
C# 高德地图WebApi对接示例
1、登录或注册高德地图开放平台然后申请应用key(需要认证个人或企业开发者) 高德开放平台 | 高德地图API (amap.com) 2、创建新应用,为新应用添加key,完成第一项后即可看到key管理 3、具体的开发文档、接口入参出参以及结果示例等详见高德地图开放平台官网 地理/逆地理编码-API文档-开发指南-Web服务 API | 高德地图API (amap.com) 4、直接书写具体示例 开发语言:C# 开发工具:visual studio 2019 开发项目类型:控制台程序 //
154 1
C# 高德地图WebApi对接示例