作者ryanwang (irene)
看板C_Sharp
标题Re: [问题] 请问socket封包到本机网卡要如何测量时间
时间Tue Oct 31 10:24:26 2023
※ 引述《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} 毫秒");
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.164.231.183 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1698719068.A.52D.html
1F:推 wolfram: 这StopWatch可量测Send指令时间,如果原Po 想更精确,需 12/31 14:28
2F:→ wolfram: 从WireShark去看127.0.0.1的反应纪录 12/31 14:28