Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Client.hpp>
- using namespace Qt;
- Client::Client(const QString& strHost, quint16 nPort, QWidget* pwgt) : QWidget(pwgt), sizeOfData(0) {
- pTcpSocket = new QTcpSocket(this);
- pTcpSocket->connectToHost(strHost, nPort);
- connect(pTcpSocket, SIGNAL(connected()), SLOT(slotConnected()));
- connect(pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
- connect(pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
- pText = new QTextEdit;
- pInput = new QLineEdit;
- pText->setReadOnly(true);
- QPushButton* pbtn = new QPushButton("&Send");
- connect(pbtn, SIGNAL(clicked()), SLOT(slotSendToServer()));
- connect(pInput, SIGNAL(returnPressed()), this, SLOT(slotSendToServer()));
- QVBoxLayout* pVboxLayout = new QVBoxLayout;
- pVboxLayout->addWidget(new QLabel("<h1>Client</h1>"));
- pVboxLayout->addWidget(pText);
- pVboxLayout->addWidget(pInput);
- pVboxLayout->addWidget(pbtn);
- setLayout(pVboxLayout);
- }
- bool Client::slotReadyRead () {
- if (pTcpSocket->bytesAvailable() <= 0) return false;
- QDataStream in(pTcpSocket);
- in.setVersion(QDataStream::Qt_5_3);
- QList<QString> results;
- QByteArray imgs2;
- //QByteArray imgs2;
- //if (pTcpSocket->bytesAvailable() == 0) break;
- //else qDebug()<<"Yes";
- if (!sizeOfData) {
- in>>sizeOfData;
- flag = false;
- }
- else if(sizeOfData != 0) {
- qDebug()<<"Размер: "<<pTcpSocket->size();
- qDebug()<<"Переменная: "<<sizeOfData;
- sizeOfData -= pTcpSocket->size();
- in>>imgs;
- if (!(imgs.isEmpty())) imgs2 += imgs;
- qDebug()<<sizeOfData;
- if (sizeOfData <= 0) flag = true;
- }
- if (flag) {
- QPixmap pix;
- pix.loadFromData(QByteArray::fromBase64(imgs2));
- test.setPixmap(pix);
- test.show();
- flag = false;
- }
- return true;
- }
- void Client::slotError(QAbstractSocket::SocketError err) {
- 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()));
- pText->append(strError);
- }
- void Client::slotSendToServer() {
- QByteArray arrBlock;
- QDataStream out(&arrBlock, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_5_3);
- out << quint16(0)<<pInput->text();
- out.device()->seek(0);
- out <<quint16(arrBlock.size() - sizeof(quint16));
- pTcpSocket->write(arrBlock);
- pInput->setText("");
- }
- void Client::slotConnected() {
- pText->append("Received the connected() signal");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement