[英]Receive Structed data from UDP connection
I use Kolor Application for play 3D movie.this application send player status on the UDP port. 我使用Kolor应用程序播放3D电影。此应用程序通过UDP端口发送播放器状态。
UDP messages can be used by any other application. UDP消息可以由任何其他应用程序使用。 For example, a 3D audio engine can use these informations to produce 3D sound in accordance with video playback made by Kolor Eyes.
例如,3D音频引擎可以根据Kolor Eyes进行的视频播放,使用这些信息来产生3D声音。
UDP messages are in JSON format ( http://www.json.org/ ). UDP消息采用JSON格式( http://www.json.org/ )。 So you have to use a JSON parser to decode the message.
因此,您必须使用JSON解析器来解码消息。
Here is the current structure of UDP message : 这是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
I create c# application To receive data from UDP port and convert this to ASCII String 我创建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);
}
But output result in console show incorrect result 但是控制台中的输出结果显示不正确的结果
qbjs☺ E ☼ ¬ >☻ ☻ id♥ ked ↕♣ ♣ pitch U← position ' ♦ roll
♥ url ) file:///C:/Users/iman/Desktop/FIN_hi2.mp4 '¶ ♥ yaw ♀ ∟
0 @ T ` ~
The messages sent by KolorEyes are not encoded as JSON, but some binary derivative. KolorEyes发送的消息未编码为JSON,而是某些二进制派生类。
It may be Qt Binary Json (qbjs), but I cannot find much info about that.
可能是Qt Binary Json(qbjs),但我找不到很多有关此的信息。
Qt5 internally transform JSON documents into a binary representation, and this is what gets transmitted over UDP. Qt5在内部将JSON文档转换为二进制表示形式,这就是通过UDP传输的内容。 http://doc.qt.io/qt-5/qjsondocument.html
http://doc.qt.io/qt-5/qjsondocument.html
This gives me the JSON document, in a Qt5 app: 这在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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.