博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
qtcpsocket send and recieve the image from youself
阅读量:5161 次
发布时间:2019-06-13

本文共 2019 字,大约阅读时间需要 6 分钟。

Thanks alot all of you, especially Thiago,

I finally got it working. The trick was to read the data after the
QTcpSocket connection was closed rather than reading it when
readyRead() was emitted.
Here is my server code:
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << ui->lblScreenShot->pixmap()->toImage();
QTcpSocket *client = pixmapServer->nextPendingConnection();
connect (client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
client->write(block);
qDebug() << block.size();
client->disconnectFromHost();
It reads the QPixmap from a label, and here is the client code:
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_7);
QImage img;
in >> img;
ui->screenshotLabel->setPixmap(QPixmap::fromImage(img));
As you can see, its all so choppy, but at least I can start up from there.

Thanks.

____________________________________________-

That is correct.

But note you said "once you've read all the data". So the question is still:
how do you know that you've read it all and that no bytes remain, still to be
received by the network?
There are usually two ways of doing this (from your "programming 101" class):
- send the length in advance
- send a sentinel afterwards
C strings use a sentinel afterwards (the NUL). You can't send a specific byte
sequence since the data you're sending is arbitrary and binary. So the only
sentinel you can send is the "connection close".
Given that the OP was closing the socket just after writing the pixmap data,
then sending the length in advance is unnecessary. Conclusion:
Sender:
connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
socket->connectToHost(...);
socket->write(block);
socket->disconnectFromHost();
socket = 0;
Receiver:
socket = server->nextPendingConnection();
connect(socket, SIGNAL(disconnected()), this, SLOT(readAllData()));
void readAllData()
{
QByteArray data = socket->readAll();
delete socket;
}

转载于:https://www.cnblogs.com/Podevor/archive/2011/11/21/2788002.html

你可能感兴趣的文章
php上传
查看>>
C# redis使用
查看>>
JavaScript(第二十五天)【事件绑定及深入】
查看>>
如何快速上手SQL映射文件的编写
查看>>
Python__return
查看>>
while(s[n]) n++;
查看>>
Javascript生成二维码(QR)
查看>>
php tp3.2生成二维码
查看>>
JS实现星级评价
查看>>
Ubuntu 16.04安装RabbitVCS替代TortoiseSVN/TortoiseGit
查看>>
ZooKeeper增加Observer部署模式提高性能(转)
查看>>
JavaScript 【 IE中的XML DOM 】
查看>>
PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法...
查看>>
dotnet文件操作
查看>>
Windows 10系统出现:“出现系统还原使用的卷影复制服务无法运行...”的问题解决...
查看>>
Servlet3.0新特性
查看>>
Java的基本数据类型
查看>>
js 中的selection对象使用笔记+光标定位
查看>>
通过LINQ TO SQL类显示数据库表的数据
查看>>
MySQL8.0本地访问设置为远程访问权限
查看>>