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;
/// 用於UDP接收的網路服務類
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);
/// 傳送資訊
/// <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);
/// 開關:在監聽UDP報文階段為true,否則為false
bool IsUdpcRecvStart = false;
/// 執行緒:不斷監聽UDP報文
Thread thrRecv;
/// <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監聽器已成功關閉");
/// 接收資料
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)
ResetTextBoxDelegate resetTextBoxDelegate = ResetTextBox;
txtbox.Invoke(resetTextBoxDelegate, new object[] { txtbox });
txtbox.Text = "";
/// 關閉程式,強制退出
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
Environment.Exit(0);
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);
}
}
}