Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <Client.hpp>
  2.  
  3. using namespace Qt;
  4.  
  5. Client::Client(const QString& strHost, quint16 nPort, QWidget* pwgt) : QWidget(pwgt), sizeOfData(0) {
  6. pTcpSocket = new QTcpSocket(this);
  7. pTcpSocket->connectToHost(strHost, nPort);
  8. connect(pTcpSocket, SIGNAL(connected()), SLOT(slotConnected()));
  9. connect(pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
  10. connect(pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
  11.  
  12. pText = new QTextEdit;
  13. pInput = new QLineEdit;
  14. pText->setReadOnly(true);
  15.  
  16. QPushButton* pbtn = new QPushButton("&Send");
  17. connect(pbtn, SIGNAL(clicked()), SLOT(slotSendToServer()));
  18. connect(pInput, SIGNAL(returnPressed()), this, SLOT(slotSendToServer()));
  19.  
  20. QVBoxLayout* pVboxLayout = new QVBoxLayout;
  21. pVboxLayout->addWidget(new QLabel("<h1>Client</h1>"));
  22. pVboxLayout->addWidget(pText);
  23. pVboxLayout->addWidget(pInput);
  24. pVboxLayout->addWidget(pbtn);
  25. setLayout(pVboxLayout);
  26. }
  27.  
  28. bool Client::slotReadyRead () {
  29. if (pTcpSocket->bytesAvailable() <= 0) return false;
  30. QDataStream in(pTcpSocket);
  31. in.setVersion(QDataStream::Qt_5_3);
  32. QList<QString> results;
  33. QByteArray imgs2;
  34. //QByteArray imgs2;
  35. //if (pTcpSocket->bytesAvailable() == 0) break;
  36. //else qDebug()<<"Yes";
  37. if (!sizeOfData) {
  38. in>>sizeOfData;
  39. flag = false;
  40. }
  41. else if(sizeOfData != 0) {
  42. qDebug()<<"Размер: "<<pTcpSocket->size();
  43. qDebug()<<"Переменная: "<<sizeOfData;
  44. sizeOfData -= pTcpSocket->size();
  45. in>>imgs;
  46. if (!(imgs.isEmpty())) imgs2 += imgs;
  47. qDebug()<<sizeOfData;
  48. if (sizeOfData <= 0) flag = true;
  49. }
  50. if (flag) {
  51. QPixmap pix;
  52. pix.loadFromData(QByteArray::fromBase64(imgs2));
  53. test.setPixmap(pix);
  54. test.show();
  55. flag = false;
  56. }
  57. return true;
  58. }
  59.  
  60. void Client::slotError(QAbstractSocket::SocketError err) {
  61. QString strError = "Error: " + (err == QAbstractSocket::HostNotFoundError ? "The host was not fount." : err == QAbstractSocket::RemoteHostClosedError ? "The remote host is closed." : err == QAbstractSocket::ConnectionRefusedError ? "The connection was refused." : QString(pTcpSocket->errorString()));
  62. pText->append(strError);
  63. }
  64.  
  65. void Client::slotSendToServer() {
  66. QByteArray arrBlock;
  67. QDataStream out(&arrBlock, QIODevice::WriteOnly);
  68. out.setVersion(QDataStream::Qt_5_3);
  69. out << quint16(0)<<pInput->text();
  70. out.device()->seek(0);
  71. out <<quint16(arrBlock.size() - sizeof(quint16));
  72. pTcpSocket->write(arrBlock);
  73. pInput->setText("");
  74. }
  75.  
  76. void Client::slotConnected() {
  77. pText->append("Received the connected() signal");
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement