【发布时间】:2015-08-11 05:27:47
【问题描述】:
我正在尝试与精确时间协议 (PTP) 服务器通信并使用 Windows 窗体和 C# 构建 PTP 时钟。我了解 Sync 消息的整个过程,有时是后续消息,然后是延迟请求消息,最后是延迟响应消息。现在我需要与服务器通信。 WireShark 会提取我需要的所有数据包,但我如何使用 C# 提取这些数据包?
我了解多播是通过 PTP 端口 319 上的 IP 地址 224.0.1.129 完成的。 我的粗略轮廓是这样的:
while (true) //Continuously getting the accurate time
{
if (Receive())
{
//create timestamp of received time
//extract timestamp of sent time
//send delay request
//Receive timestamp
//create receive timestamp
//calculate round trip time
//adjust clock to new time
}
}
private bool Receive()
{
bool bReturn = false;
int port = 319;
string m_serverIP = "224.0.1.129";
byte[] packetData = new byte[86];
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(m_serverIP), port);
Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
newSocket.Connect(ipEndPoint);
newSocket.ReceiveTimeout = 3000;
newSocket.Receive(packetData, SocketFlags.None);
newSocket.Close();
bReturn = true;
}
catch
{ }
return bReturn;
}
其中 Receive() 是一个方法,如果您收到同步消息,则返回布尔值,并最终将消息存储在字节中。我正在尝试使用套接字与服务器连接,但我的计时器总是超时,并返回 false。我将 PTP 服务器设置为每秒发送一条同步消息,所以我知道我的超时(3 秒后)应该能够接收到它。
请帮忙!
【问题讨论】:
标签: c# sockets visual-studio-2013