如图点击右侧的按钮,第一次开始监听,第二次终止监听.在监听的状态下,在左侧输入文本,点击左侧发送数据按钮,右边的文本框会显示左边发送的数据.
源码:
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
using System.Windows.Forms;
-
using System.Net.Sockets;
-
using System.Net;
-
using System.Threading;
-
namespace _02UDP通信
-
{
-
public partial
class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
/// <summary>
-
/// 用于UDP发送的网络服务类
-
/// </summary>
-
private UdpClient udpcSend;
-
/// <summary>
-
/// 用于UDP接收的网络服务类
-
/// </summary>
-
private UdpClient udpcRecv;
-
private void btnSend_Click(object sender, EventArgs e)
-
{
-
if (
string.IsNullOrWhiteSpace(txtSendMsg.Text))
-
{
-
MessageBox.Show(
"请先输入待发送内容");
-
return;
-
}
-
-
// 匿名发送
-
//udpcSend = new UdpClient(0); // 自动分配本地IPv4地址
-
-
// 实名发送
-
IPEndPoint localIpep =
new IPEndPoint(IPAddress.Parse(
"127.0.0.1"),
25555);
// 本机IP,指定的端口号
-
udpcSend =
new UdpClient(localIpep);
-
Thread thrSend =
new Thread(SendMessage);
-
-
thrSend.Start(txtSendMsg.Text);
-
-
}
-
/// <summary>
-
/// 发送信息
-
/// </summary>
-
/// <param name="o"></param>
-
void SendMessage(object o)
-
{
-
string message = (
string)o;
-
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
-
IPEndPoint remoteIpep =
new IPEndPoint(IPAddress.Parse(
"127.0.0.1"),
8848);
// 发送到的IP地址和端口号
-
udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
-
udpcSend.Close();
-
ResetTextBox(txtSendMsg);
-
}
-
/// <summary>
-
/// 开关:在监听UDP报文阶段为true,否则为false
-
/// </summary>
-
bool IsUdpcRecvStart =
false;
-
/// <summary>
-
/// 线程:不断监听UDP报文
-
/// </summary>
-
Thread thrRecv;
-
/// <summary>
-
/// 按钮:接收数据开关
-
/// </summary>
-
/// <param name="sender"></param>
-
/// <param name="e"></param>
-
private void btnRecv_Click(object sender, EventArgs e)
-
{
-
if (!IsUdpcRecvStart)
// 未监听的情况,开始监听
-
{
-
IPEndPoint localIpep =
new IPEndPoint(IPAddress.Parse(
"127.0.0.1"),
8848);
// 本机IP和监听端口号
-
udpcRecv =
new UdpClient(localIpep);
-
thrRecv =
new Thread(ReceiveMessage);
-
thrRecv.Start();
-
IsUdpcRecvStart =
true;
-
ShowMessage(txtRecvMsg,
"UDP监听器已成功启动");
-
}
-
else
// 正在监听的情况,终止监听
-
{
-
thrRecv.Abort();
// 必须先关闭这个线程,否则会异常
-
udpcRecv.Close();
-
IsUdpcRecvStart =
false;
-
ShowMessage(txtRecvMsg,
"UDP监听器已成功关闭");
-
}
-
}
-
/// <summary>
-
/// 接收数据
-
/// </summary>
-
/// <param name="o"></param>
-
void ReceiveMessage(object o)
-
{
-
IPEndPoint remoteIpep =
new IPEndPoint(IPAddress.Any,
0);
-
while (
true)
-
{
-
try
-
{
-
byte[] bytRecv = udpcRecv.Receive(ref remoteIpep);
-
string message = Encoding.Unicode.GetString( bytRecv,
0, bytRecv.Length);
-
ShowMessage(txtRecvMsg,
string.Format(
"{0}[{1}]", remoteIpep, message));
-
}
-
catch (Exception ex)
-
{
-
ShowMessage(txtRecvMsg, ex.Message);
-
break;
-
}
-
}
-
}
-
// 向TextBox中添加文本
-
delegate void ShowMessageDelegate(TextBox txtbox, string message);
-
void ShowMessage(TextBox txtbox, string message)
-
{
-
if (txtbox.InvokeRequired)
-
{
-
ShowMessageDelegate showMessageDelegate = ShowMessage;
-
txtbox.Invoke(showMessageDelegate,
new object[] { txtbox, message });
-
}
-
else
-
{
-
txtbox.Text += message +
"\r\n";
-
}
-
}
-
// 清空指定TextBox中的文本
-
delegate void ResetTextBoxDelegate(TextBox txtbox);
-
void ResetTextBox(TextBox txtbox)
-
{
-
if (txtbox.InvokeRequired)
-
{
-
ResetTextBoxDelegate resetTextBoxDelegate = ResetTextBox;
-
txtbox.Invoke(resetTextBoxDelegate,
new object[] { txtbox });
-
}
-
else
-
{
-
txtbox.Text =
"";
-
}
-
}
-
/// <summary>
-
/// 关闭程序,强制退出
-
/// </summary>
-
/// <param name="sender"></param>
-
/// <param name="e"></param>
-
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
-
{
-
Environment.Exit(
0);
-
}
-
}
-
}