C#利用广播实现群发功能

C#利用广播实现群发功能

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. //添加的命名空间引用  
  9. using System.Net;  
  10. using System.Net.Sockets;  
  11. using System.Threading;  
  12. namespace BroadcastExample  
  13. {  
  14.     public partial class FormBroacast : Form  
  15.     {  
  16.         delegate void AppendStringCallback(string text);  
  17.         AppendStringCallback appendStringCallback;  
  18.         //使用的接收端口号  
  19.         private int port = 8001;  
  20.         private UdpClient udpClient;  
  21.         public FormBroacast()  
  22.         {  
  23.             InitializeComponent();  
  24.             appendStringCallback = new AppendStringCallback(AppendString);  
  25.         }  
  26.   
  27.         private void FormBroacast_Load(object sender, EventArgs e)  
  28.         {  
  29.             Thread myThread = new Thread(ReceiveData);  
  30.             //将线程设为后台运行  
  31.             myThread.IsBackground = true;  
  32.             myThread.Start();  
  33.         }  
  34.   
  35.         
  36.         /// <summary>  
  37.         /// 在后台运行的接收线程  
  38.         /// </summary>  
  39.         private void ReceiveData()  
  40.         {  
  41.             //在本机指定的端口接收  
  42.             udpClient = new UdpClient(port);  
  43.             IPEndPoint remote = null;  
  44.             //接收从远程主机发送过来的信息;  
  45.             while (true)  
  46.             {  
  47.                 try  
  48.                 {  
  49.                     //关闭udpClient时此句会产生异常  
  50.                     byte[] bytes = udpClient.Receive(ref remote);  
  51.                     string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
  52.                     AppendString(string.Format("来自{0}:{1}", remote, str));  
  53.                 }  
  54.                 catch  
  55.                 {  
  56.                     //退出循环,结束线程  
  57.                     break;  
  58.                 }  
  59.             }  
  60.         }  
  61.   
  62.         /// <summary>  
  63.         /// 发送消息  
  64.         /// </summary>  
  65.         /// <param name="sender"></param>  
  66.         /// <param name="e"></param>  
  67.         private void buttonSend_Click(object sender, EventArgs e)  
  68.         {  
  69.             UdpClient myUdpClient = new UdpClient();  
  70.             try  
  71.             {  
  72.                 //让其自动提供子网中的IP广播地址  
  73.                 IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 8001);  
  74.                 //将发送内容转换为字节数组  
  75.                 byte[] bytes =Encoding.UTF8.GetBytes(textBox1.Text);  
  76.                 //向子网发送信息  
  77.                 myUdpClient.Send(bytes, bytes.Length, iep);  
  78.                 textBox1.Clear();  
  79.                 textBox1.Focus();  
  80.             }  
  81.             catch (Exception err)  
  82.             {  
  83.                 MessageBox.Show(err.Message, "发送失败");  
  84.             }  
  85.             finally  
  86.             {  
  87.                 myUdpClient.Close();  
  88.             }  
  89.         }  
  90.          
  91.         private void FormBroacast_FormClosing(object sender, FormClosingEventArgs e)  
  92.         {  
  93.             udpClient.Close();  
  94.         }  
  95.   
  96.         private void AppendString(string text)  
  97.         {  
  98.             if (richTextBox1.InvokeRequired == true)  
  99.             {  
  100.                 this.Invoke(appendStringCallback, text);  
  101.             }  
  102.             else  
  103.             {  
  104.                 richTextBox1.AppendText(text + "\r\n");  
  105.             }  
  106.         }  
  107.     }  
  108. }  

界面设计:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值