【问题标题】:Receive Structed data from UDP connection从 UDP 连接接收结构化数据
【发布时间】:2015-07-10 17:55:00
【问题描述】:

我使用 Kolor 应用程序播放 3D 电影。此应用程序通过 UDP 端口发送播放器状态。

UDP 消息可以被任何其他应用程序使用。例如,3D 音频引擎可以使用这些信息来根据 Kolor Eyes 制作的视频播放产生 3D 声音。

UDP 消息采用 JSON 格式 (http://www.json.org/)。所以你必须使用 JSON 解析器来解码消息。

这是UDP消息的当前结构:

"id": "ked" --- message identifier
"yaw": float --- yaw in radians
"pitch": float --- pitch in radians
"roll": float --- roll in radians
"url": string --- current video url
"state": enum --- playback state, integer possible values are : 0 (StoppedState), 1 (PlayingState), 2 (PausedState)
"position": int --- current video playback position in milliseconds

我创建 c# 应用程序从 UDP 端口接收数据并将其转换为 ASCII 字符串

static void Main(string[] args)
{         
    int localPort = 7755;
    IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);                 
    // Create UDP client
    UdpClient client = new UdpClient(localPort);
    UdpState state = new UdpState(client, remoteSender);
    // Start async receiving
    client.BeginReceive(new AsyncCallback(DataReceived), state);
    // Wait for any key to terminate application
    Console.ReadKey();
    client.Close();
}
private static void DataReceived(IAsyncResult ar)
{
    UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
    IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
    IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);

    // Check sender
    bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
    bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;
    if (isRightHost && isRightPort)
    {               
        string receivedText = Encoding.Default.GetString(receyiveBytes);
        Console.WriteLine(receivedText);
    }
    // Restart listening for udp data packages
    c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);

}

但控制台输出结果显示不正确

  qbjs☺   E   ☼   ¬   >☻  ☻ id♥ ked   ↕♣  ♣ pitch         U← position  '    ♦ roll
  ♥ url   ) file:///C:/Users/iman/Desktop/FIN_hi2.mp4 '¶  ♥ yaw           ♀   ∟
  0   @   T   `   ~

【问题讨论】:

  • @Shekhar 我想让 C# 应用程序从 udp 接收这些数据
  • 我知道你想要什么,但它仍然没有回答你尝试了什么?
  • 我创建了一个应用程序来从 udp 端口​​接收数据并有一个字节数组,但知道如何将字节数组转换为我的数据结构
  • @Shekhar 我编辑我的问题并将我的源代码添加到此请查看此代码谢谢
  • 尝试将Encoding.Default 更改为Encoding.ASCIIUTF8

标签: c# .net json


【解决方案1】:

KolorEyes 发送的消息不是编码为 JSON,而是一些二进制派生的。 可能是 Qt Binary Json (qbjs),但我找不到太多相关信息。

Qt5 在内部将 JSON 文档转换为二进制表示,这就是通过 UDP 传输的内容。 http://doc.qt.io/qt-5/qjsondocument.html

这给了我一个 Qt5 应用程序中的 JSON 文档:

void MainWindow::initSocket()
{
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::LocalHost, 7755);

    connect(udpSocket, SIGNAL(readyRead()),
            this, SLOT(readPendingDatagrams()));
}

void MainWindow::readPendingDatagrams()
{
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        udpSocket->readDatagram(datagram.data(), datagram.size(),
                            &sender, &senderPort);

        QJsonDocument document = QJsonDocument::fromBinaryData(datagram);
        qDebug() << document;
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多