1、文件结构
2、UdpReceiver.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace UDPTest.Udp
{
public class UdpStateEventArgs : EventArgs
{
public IPEndPoint remoteEndPoint;
public byte[] buffer = null;
}
public delegate void UdpReceivedEventHandler(UdpStateEventArgs args);
public class UdpReceiver
{
private UdpClient udpClient;
public event UdpReceivedEventHandler UdpReceived;
public UdpReceiver(string localIp,int localPort, string remoteIp, int remotePort)
{
IPEndPoint localPoint = new IPEndPoint(IPAddress.Parse(localIp), localPort);
udpClient = new UdpClient(localPoint);
Task.Run(() =>
{
while (true)
{
UdpStateEventArgs args = new UdpStateEventArgs();
if (udpClient != null)
{
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), remotePort);
var received = udpClient.Receive(ref remotePoint);
args.remoteEndPoint = remotePoint;
args.buffer = received;
UdpReceived?.Invoke(args);
}
else
{
break;
}
}
});
}
}
}
3、ControlCross.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UDPTest.Utils
{
internal class ControlCross
{
public static void LableSetText(Label label, string text)
{
if (label.InvokeRequired)
{
MethodInvoker mi = delegate ()
{
label.Text = text;
};
label.Invoke(mi);
}
else
{
label.Text = text;
}
}
}
}
4、MainForm窗口
5、MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UDPTest.Udp;
using UDPTest.Utils;
namespace UDPTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
string localIp="127.0.0.1";
int localPort = 7777;
string remoteIp = "127.0.0.1";
int remotePort = 8081;
string remotePointInfo = "";
string receiveBuffer = "";
private void btStartTest_Click(object sender, EventArgs e)
{
UdpReceiver udpReceiver=new UdpReceiver(localIp, localPort, remoteIp, remotePort);
udpReceiver.UdpReceived += UdpReceiverProcess;
}
private void UdpReceiverProcess(UdpStateEventArgs args)
{
var remotePoint = args.remoteEndPoint;
remotePointInfo = $"{remotePoint.Address}:{remotePoint.Port}";
receiveBuffer = Encoding.UTF8.GetString(args.buffer);
ControlCross.LableSetText(lbTest1, remotePointInfo);
ControlCross.LableSetText(lbTest2, receiveBuffer);
}
private UdpClient udpClient = null;
private void btSendSet_Click(object sender, EventArgs e)
{
IPEndPoint localPoint = new IPEndPoint(IPAddress.Parse(localIp), localPort);
udpClient = new UdpClient(localPoint);
}
private void btSendTest_Click(object sender, EventArgs e)
{
string str=this.tbSendData.Text;
byte[] buffer=Encoding.UTF8.GetBytes(str);
if(udpClient != null)
{
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), remotePort);
udpClient.Send(buffer,buffer.Length,remotePoint);
}
}
}
}
6、运行代码