[英]Determine Data Type on a TCP Socket in Qt
I am writing a program to send images captured from an OpenCV window over a TCP connection, using Qt libraries to setup the connections etc. 我正在编写一个程序,使用Qt库设置连接等,以通过TCP连接发送从OpenCV窗口捕获的图像。
I have to functions (below) which are both working to send either text or a byte array. 我必须使用下面的函数来发送文本或字节数组。 The problem I have is at the other end how can I tell if the data coming in is plain text, or an array containing an image.
我遇到的问题是在另一端,如何确定输入的数据是纯文本还是包含图像的数组。 Is there an inbuilt way to do this, or do I need to put a byte at the start of the data to tell the receiver what data is coming?
是否有内置的方法来执行此操作,还是我需要在数据的开头放置一个字节以告知接收器要接收哪些数据? I already put the array length at the start of the serialized image data.
我已经将数组长度放在序列化图像数据的开头。
void Screenshot_controller::sendText(std::string textToSend)
{
if(connectionMade)
{
std::string endLine = "\r\n";
textToSend = textToSend + endLine;
const char * textChar = textToSend.c_str();
sendSocket->write(textChar);
sendSocket->flush();
qDebug() << "Text Sent from Server";
}
}
void Screenshot_controller::sendData(QByteArray dataToSend)
{
if(connectionMade)
{
sendSocket->write(dataToSend);
sendSocket->flush();
qDebug() << "Data Sent from Server";
}
}
You need to define the protocol yourself, whether that's with a byte, string, JSON header or any other method. 您需要自己定义协议,无论是使用字节,字符串,JSON标头还是任何其他方法。 The Tcp socket will allow you to transfer the data, but doesn't care what that data is;
Tcp套接字将允许您传输数据,但不在乎该数据是什么。 it's up to you to handle that.
由您自己决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.