在C#中,DatagramPacket 是 Java 中用于 UDP 通信的一个类,而 C# 并没有直接对应的 DatagramPacket 类。不过,C# 提供了类似的机制来处理基于 UDP 的数据报(datagram)通信,主要通过 System.Net.Sockets 命名空间中的 UdpClient 和 Socket 类来实现
UdpClient是相对于Socket更高级的类,适合简单的UDP通信。它提供了发送和接收的数据报文的方法,并且可以提供一些自动处理底层的细节。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main()
{
// 创建 UdpClient 实例
using (UdpClient udpClient = new UdpClient())
{
// 指定目标 IP 地址和端口
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
// 要发送的数据
string message = "Hello, UDP!";
byte[] data = Encoding.UTF8.GetBytes(message);
// 发送数据报
udpClient.Send(data, data.Length, remoteEndPoint);
Console.WriteLine("Data sent to: " + remoteEndPoint);
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main()
{
// 创建 UdpClient 实例并绑定到本地端口
using (UdpClient udpClient = new UdpClient(12345))
{
Console.WriteLine("Waiting for data...");
// 接收数据报
UdpReceiveResult result = udpClient.ReceiveAsync().GetAwaiter().GetResult();
byte[] receivedData = result.Buffer;
IPEndPoint remoteEndPoint = result.RemoteEndPoint;
// 将接收到的字节数组转换为字符串
string message = Encoding.UTF8.GetString(receivedData);
Console.WriteLine($"Received from {remoteEndPoint}: {message}");
}
}
}
Socket类提供了更低级别的控制,可以更小粒度的控制本身的逻辑,适合用于更复杂的Udp通信场景,你可以使用Socket类来创建,发送和接收数据报。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main()
{
// 创建 Socket 实例
using (Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
// 指定目标 IP 地址和端口
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
// 要发送的数据
string message = "Hello, UDP!";
byte[] data = Encoding.UTF8.GetBytes(message);
// 发送数据报
udpSocket.SendTo(data, remoteEndPoint);
Console.WriteLine("Data sent to: " + remoteEndPoint);
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main()
{
// 创建 Socket 实例并绑定到本地端口
using (Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 12345);
udpSocket.Bind(localEndPoint);
Console.WriteLine("Waiting for data...");
// 接收数据报
byte[] buffer = new byte[1024];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int receivedBytes = udpSocket.ReceiveFrom(buffer, ref remoteEndPoint);
// 将接收到的字节数组转换为字符串
string message = Encoding.UTF8.GetString(buffer, 0, receivedBytes);
Console.WriteLine($"Received from {remoteEndPoint}: {message}");
}
}
}
UdpClient:适合简单的 UDP 通信,提供了更高层次的抽象,易于使用。
Socket:提供了更低级别的控制,适合需要更复杂功能的场景,如多播、广播等。
IPEndPoint:表示 IP 地址和端口号,用于指定发送和接收的目标地址。
Encoding.UTF8.GetBytes 和 Encoding.UTF8.GetString:用于将字符串转换为字节数组,或将字节数组转换为字符串。你可以根据需要选择不同的编码方式(如 UTF-8、ASCII 等)。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
// 创建 UdpClient 实例
using (UdpClient udpClient = new UdpClient())
{
// 指定目标 IP 地址和端口
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
// 要发送的数据
string message = "Hello, UDP!";
byte[] data = Encoding.UTF8.GetBytes(message);
// 异步发送数据报
await udpClient.SendAsync(data, data.Length, remoteEndPoint);
Console.WriteLine("Data sent to: " + remoteEndPoint);
// 异步接收数据报
UdpReceiveResult result = await udpClient.ReceiveAsync();
byte[] receivedData = result.Buffer;
IPEndPoint receivedEndPoint = result.RemoteEndPoint;
// 将接收到的字节数组转换为字符串
string receivedMessage = Encoding.UTF8.GetString(receivedData);
Console.WriteLine($"Received from {receivedEndPoint}: {receivedMessage}");
}
}
}
在 C# 中,虽然没有直接的 DatagramPacket 类,但你可以使用 UdpClient 或 Socket 类来实现类似的功能。UdpClient 适合简单的 UDP 通信,而 Socket 提供了更多的灵活性和控制。根据你的需求选择合适的方式,并考虑使用异步方法来提高性能。