楼主:
ryanwang (irene)
2023-10-31 10:24:26※ 引述《sam319 (Sam)》之铭言:
: 从socket.Send(data)一刻起
: 到本机网卡收到data但还没传到远端之前
: 请问C#有办法测量这段时间吗?
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
class Program
{
static void Main()
{
// 设定目标 IP 和 Port,这个例子中使用本机 IP 和一个特定 Port
IPAddress localIpAddress = IPAddress.Parse("127.0.0.1");
int port = 12345;
// 创建一个 UdpClient 来发送封包
using (UdpClient udpClient = new UdpClient())
{
// 要发送的资料
byte[] data = new byte[1024]; // 假设封包大小为 1024 字节
// 开始计时
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 发送封包
udpClient.Send(data, data.Length, new IPEndPoint(localIpAddress,
port));
// 停止计时
stopwatch.Stop();
// 显示所需时间
Console.WriteLine($"传送封包所需时间:
{stopwatch.ElapsedMilliseconds} 毫秒");
}
}
}