c# UDP通过广播实现群发功能
2011-06-29 16:00
447 查看
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;
//UDP通过广播实现群发功能
namespace BroadcastExample
{
public partial class Form1 : Form
{
delegate void AppendStringCallback(string text);
AppendStringCallback appendstringcallback;
//使用的接收端口 51008
/// <summary>
/// 端口号
/// </summary>
private int port = 51008;
/// <summary>
/// udp连接对象
/// </summary>
private UdpClient udpclient;
public Form1()
{
InitializeComponent();
appendstringcallback = new AppendStringCallback(AppendString);
}
/// <summary>
/// 委托对象的处理过程
/// </summary>
/// <param name="text"></param>
private void AppendString(string text)
{
if (richtextBox2.InvokeRequired == true)
{
this.Invoke(appendstringcallback, text);
}
else
{
richtextBox2.AppendText(text + "/r/n");
}
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
private void RecData()
{
//本机指定端口接收
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;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//创建一个线程接收接收远程主机发来的信息
Thread mythread = new Thread(new ThreadStart(RecData));
//将线程设为后台运行
mythread.IsBackground = true;
mythread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
udpclient.Close();
}
private void button1_Click(object sender, EventArgs e)
{
UdpClient myUdpclient = new UdpClient();
try
{
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
myUdpclient.Send(bytes, bytes.Length, iep);
textBox1.Clear();
myUdpclient.Close();
textBox1.Focus();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
myUdpclient.Close();
}
}
}
}
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;
//UDP通过广播实现群发功能
namespace BroadcastExample
{
public partial class Form1 : Form
{
delegate void AppendStringCallback(string text);
AppendStringCallback appendstringcallback;
//使用的接收端口 51008
/// <summary>
/// 端口号
/// </summary>
private int port = 51008;
/// <summary>
/// udp连接对象
/// </summary>
private UdpClient udpclient;
public Form1()
{
InitializeComponent();
appendstringcallback = new AppendStringCallback(AppendString);
}
/// <summary>
/// 委托对象的处理过程
/// </summary>
/// <param name="text"></param>
private void AppendString(string text)
{
if (richtextBox2.InvokeRequired == true)
{
this.Invoke(appendstringcallback, text);
}
else
{
richtextBox2.AppendText(text + "/r/n");
}
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
private void RecData()
{
//本机指定端口接收
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;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//创建一个线程接收接收远程主机发来的信息
Thread mythread = new Thread(new ThreadStart(RecData));
//将线程设为后台运行
mythread.IsBackground = true;
mythread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
udpclient.Close();
}
private void button1_Click(object sender, EventArgs e)
{
UdpClient myUdpclient = new UdpClient();
try
{
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
myUdpclient.Send(bytes, bytes.Length, iep);
textBox1.Clear();
myUdpclient.Close();
textBox1.Focus();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
myUdpclient.Close();
}
}
}
}
相关文章推荐
- c# UDP通过广播实现群发功能
- c# UDP通过广播实现群发功能
- C#利用广播实现群发功能
- C#利用广播实现群发功能
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
- C#实现局域网UDP广播------C#网络编程杂记[1]
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
- C#实现局域网UDP广播--
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
- 关于通过发送广播实现当前账号退出登录功能时需要注意的一些
- C#实现UDP广播
- C# 软件下载插件,软件自动更新功能实现,通过cmd命令调用应用程序,应用程序实现单例启动
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
- Android通过广播事件实现退出程序功能。
- Python+Socket实现基于UDP协议的局域网广播功能示例
- Android通过广播实现强制下线功能
- 网络编程(学习整理)---3--(Udp)FeiQ实现广播消息群发
- C#实现局域网UDP广播
- C# UDP编程(通过类UdpClient实现收发)