Send/Receive QImage with QTcpSocket
up vote
0
down vote
favorite
I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.
Here is my code for sending ..
void CameraServer::sendData(const QImage &img)
{
if (mTcpClient->state() == QTcpSocket::ConnectedState) {
QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());
// init
QByteArray data;
QDataStream outStream(&data, QIODevice::WriteOnly);
outStream.setDevice(mTcpClient);
outStream.setVersion(QDataStream::Qt_5_11);
// serialize
outStream << imgArray.size();
outStream << imgArray;
// send
mTcpClient.write(data);
mTcpClient->waitForBytesWritten();
mTcpClient->flush();
qDebug() << "Sent" << data.size() << "bytes";
}
.. and for reading.
void VCameraClient::readData()
{
qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";
if (size == 0) {
// read the image size
// toBeRead is a class private member
inputStream >> toBeRead;
}
// read partial data
// byteArr is a class private member
if (mTcpSocket->bytesAvailable() < toBeRead) {
byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));
// update the size to be read
toBeRead -= byteArr.size();
}
// read completed
if (toBeRead <= 0) {
mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
emit newFrameRead(mFrame);
}
}
Could anyone help me, please? How to use QDataStream transactions properly?
ipc qimage qtcpsocket qtcpserver
add a comment |
up vote
0
down vote
favorite
I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.
Here is my code for sending ..
void CameraServer::sendData(const QImage &img)
{
if (mTcpClient->state() == QTcpSocket::ConnectedState) {
QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());
// init
QByteArray data;
QDataStream outStream(&data, QIODevice::WriteOnly);
outStream.setDevice(mTcpClient);
outStream.setVersion(QDataStream::Qt_5_11);
// serialize
outStream << imgArray.size();
outStream << imgArray;
// send
mTcpClient.write(data);
mTcpClient->waitForBytesWritten();
mTcpClient->flush();
qDebug() << "Sent" << data.size() << "bytes";
}
.. and for reading.
void VCameraClient::readData()
{
qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";
if (size == 0) {
// read the image size
// toBeRead is a class private member
inputStream >> toBeRead;
}
// read partial data
// byteArr is a class private member
if (mTcpSocket->bytesAvailable() < toBeRead) {
byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));
// update the size to be read
toBeRead -= byteArr.size();
}
// read completed
if (toBeRead <= 0) {
mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
emit newFrameRead(mFrame);
}
}
Could anyone help me, please? How to use QDataStream transactions properly?
ipc qimage qtcpsocket qtcpserver
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.
Here is my code for sending ..
void CameraServer::sendData(const QImage &img)
{
if (mTcpClient->state() == QTcpSocket::ConnectedState) {
QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());
// init
QByteArray data;
QDataStream outStream(&data, QIODevice::WriteOnly);
outStream.setDevice(mTcpClient);
outStream.setVersion(QDataStream::Qt_5_11);
// serialize
outStream << imgArray.size();
outStream << imgArray;
// send
mTcpClient.write(data);
mTcpClient->waitForBytesWritten();
mTcpClient->flush();
qDebug() << "Sent" << data.size() << "bytes";
}
.. and for reading.
void VCameraClient::readData()
{
qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";
if (size == 0) {
// read the image size
// toBeRead is a class private member
inputStream >> toBeRead;
}
// read partial data
// byteArr is a class private member
if (mTcpSocket->bytesAvailable() < toBeRead) {
byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));
// update the size to be read
toBeRead -= byteArr.size();
}
// read completed
if (toBeRead <= 0) {
mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
emit newFrameRead(mFrame);
}
}
Could anyone help me, please? How to use QDataStream transactions properly?
ipc qimage qtcpsocket qtcpserver
I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.
Here is my code for sending ..
void CameraServer::sendData(const QImage &img)
{
if (mTcpClient->state() == QTcpSocket::ConnectedState) {
QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());
// init
QByteArray data;
QDataStream outStream(&data, QIODevice::WriteOnly);
outStream.setDevice(mTcpClient);
outStream.setVersion(QDataStream::Qt_5_11);
// serialize
outStream << imgArray.size();
outStream << imgArray;
// send
mTcpClient.write(data);
mTcpClient->waitForBytesWritten();
mTcpClient->flush();
qDebug() << "Sent" << data.size() << "bytes";
}
.. and for reading.
void VCameraClient::readData()
{
qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";
if (size == 0) {
// read the image size
// toBeRead is a class private member
inputStream >> toBeRead;
}
// read partial data
// byteArr is a class private member
if (mTcpSocket->bytesAvailable() < toBeRead) {
byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));
// update the size to be read
toBeRead -= byteArr.size();
}
// read completed
if (toBeRead <= 0) {
mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
emit newFrameRead(mFrame);
}
}
Could anyone help me, please? How to use QDataStream transactions properly?
ipc qimage qtcpsocket qtcpserver
ipc qimage qtcpsocket qtcpserver
edited Nov 20 at 16:18
asked Nov 19 at 15:58
gont_
35
35
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378393%2fsend-receive-qimage-with-qtcpsocket%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown