C#利用广播实现群发功能
2013-12-06 08:21
239 查看
C#利用广播实现群发功能
[csharp] view
plaincopy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//添加的命名空间引用
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace BroadcastExample
{
public partial class FormBroacast : Form
{
delegate void AppendStringCallback(string text);
AppendStringCallback appendStringCallback;
//使用的接收端口号
private int port = 8001;
private UdpClient udpClient;
public FormBroacast()
{
InitializeComponent();
appendStringCallback = new AppendStringCallback(AppendString);
}
private void FormBroacast_Load(object sender, EventArgs e)
{
Thread myThread = new Thread(ReceiveData);
//将线程设为后台运行
myThread.IsBackground = true;
myThread.Start();
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
private void ReceiveData()
{
//在本机指定的端口接收
udpClient = new UdpClient(port);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
AppendString(string.Format("来自{0}:{1}", remote, str));
}
catch
{
//退出循环,结束线程
break;
}
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSend_Click(object sender, EventArgs e)
{
UdpClient myUdpClient = new UdpClient();
try
{
//让其自动提供子网中的IP广播地址
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 8001);
//将发送内容转换为字节数组
byte[] bytes =Encoding.UTF8.GetBytes(textBox1.Text);
//向子网发送信息
myUdpClient.Send(bytes, bytes.Length, iep);
textBox1.Clear();
textBox1.Focus();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
myUdpClient.Close();
}
}
private void FormBroacast_FormClosing(object sender, FormClosingEventArgs e)
{
udpClient.Close();
}
private void AppendString(string text)
{
if (richTextBox1.InvokeRequired == true)
{
this.Invoke(appendStringCallback, text);
}
else
{
richTextBox1.AppendText(text + "\r\n");
}
}
}
}
界面设计:
[csharp] view
plaincopy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//添加的命名空间引用
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace BroadcastExample
{
public partial class FormBroacast : Form
{
delegate void AppendStringCallback(string text);
AppendStringCallback appendStringCallback;
//使用的接收端口号
private int port = 8001;
private UdpClient udpClient;
public FormBroacast()
{
InitializeComponent();
appendStringCallback = new AppendStringCallback(AppendString);
}
private void FormBroacast_Load(object sender, EventArgs e)
{
Thread myThread = new Thread(ReceiveData);
//将线程设为后台运行
myThread.IsBackground = true;
myThread.Start();
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
private void ReceiveData()
{
//在本机指定的端口接收
udpClient = new UdpClient(port);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
AppendString(string.Format("来自{0}:{1}", remote, str));
}
catch
{
//退出循环,结束线程
break;
}
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSend_Click(object sender, EventArgs e)
{
UdpClient myUdpClient = new UdpClient();
try
{
//让其自动提供子网中的IP广播地址
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 8001);
//将发送内容转换为字节数组
byte[] bytes =Encoding.UTF8.GetBytes(textBox1.Text);
//向子网发送信息
myUdpClient.Send(bytes, bytes.Length, iep);
textBox1.Clear();
textBox1.Focus();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
myUdpClient.Close();
}
}
private void FormBroacast_FormClosing(object sender, FormClosingEventArgs e)
{
udpClient.Close();
}
private void AppendString(string text)
{
if (richTextBox1.InvokeRequired == true)
{
this.Invoke(appendStringCallback, text);
}
else
{
richTextBox1.AppendText(text + "\r\n");
}
}
}
}
界面设计:
相关文章推荐
- C#利用广播实现群发功能
- c# UDP通过广播实现群发功能
- c# UDP通过广播实现群发功能
- c# UDP通过广播实现群发功能
- 利用python 实现微信公众号群发图片与文本消息功能
- C#下利用正则表达式实现字符串搜索功能的方法(转)
- c# wpf 利用截屏键实现截屏功能
- C#利用Windows自带gdi32.dll实现抓取屏幕功能实例
- 利用C#动态编译功能实现像Javascript中的Eval的功能来将一段字符串进行数学运算
- xml学习(三)---利用xml文件实现数据岛功能(网页显示xml文件中的内容免C#代码操作xml文件)
- C#利用zxing.net生成二维码和条形码并实现打印的功能
- 利用C#实现数据同步功能
- 利用C#中ToolStirp中的ComboBox控件实现下拉菜单改变文档字体的功能
- 利用C#实现生日提醒的功能
- C#利用组播实现网络会议室功能(即群聊功能)
- C#利用Zxing.net生成二维码和条形码并实现打印的功能
- C#利用摄像头拍照功能实现
- C#动态执行类库中的函数:利用反射实现(js)Eval,asp(Execute)功能
- C#GDI+实现简单视屏广播功能
- C#利用反射实现(js)Eval,asp(Execute)功能 动态执行类库中的函数