if (pTcpSocket->size() != 128) { qDebug() << in; qDebug() << "______________________________________"; in.device()->dumpObjectInfo(); qDebug() << "______________________________________"; in.device()->dumpObjectTree(); pTcpSocket->disconnect(); return; }
QTcpSocket tcpSocket; if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { qDebug() << "TCPSocketError" << tcpSocket.error(); emit error(tcpSocket.error()); return; } QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_8); out.setByteOrder(QDataStream::BigEndian); out.setFloatingPointPrecision(QDataStream::SinglePrecision); DataServer *srv = (DataServer*) parent(); for(int i = 0; i < num_data; i++) {// сериализуем... } qDebug() << "sending: " << out.device()->size() << " bytes..."; tcpSocket.write(block); tcpSocket.disconnectFromHost(); if (tcpSocket.state() == QAbstractSocket::UnconnectedState || tcpSocket.waitForDisconnected(1000)) qDebug() << "sent!" << "socket state: " << tcpSocket.state();
void ServerThread::run(){ QTcpSocket tcpSocket; if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { qDebug() << "TCPSocketError" << tcpSocket.error(); emit error(tcpSocket.error()); return; } QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_8); out.setByteOrder(QDataStream::BigEndian); out.setFloatingPointPrecision(QDataStream::SinglePrecision); DataServer *srv = (DataServer*) parent(); for(int i = 0; i < num_data; i++) { out << srv->struct[i].int; out << srv->struct[i].float; out << srv->struct[i].float; out << srv->struct[i].float; } qDebug() << "sending: " << out.device()->size() << " bytes..."; tcpSocket.write(block); tcpSocket.disconnectFromHost(); if (tcpSocket.state() == QAbstractSocket::UnconnectedState || tcpSocket.waitForDisconnected(1000)) qDebug() << "sent!" << "socket state: " << tcpSocket.state();}
void IncomingDataHandler::readData(){ qDebug() << "socket data recieved: " << pTcpSocket->size() << " bytes"; QDataStream in(pTcpSocket); in.setVersion(QDataStream::Qt_4_8); in.setByteOrder(QDataStream::BigEndian); in.setFloatingPointPrecision(QDataStream::SinglePrecision); if (pTcpSocket->size() != 128) { pTcpSocket->disconnect(); return; } for(int i = 0; i < 8; i++) { in >> struct[i].int in >> struct[i].float in >> struct[i].float in >> struct[i].float } if(in.atEnd()) { qDebug() << "end of data..."; emit newData(struct*); }}
int size = pTcpSocket->size(); qDebug() << "socket data recieved: " << size << " bytes";... if (size != 128) { qDebug() << "wrong socket " << size << " : " << pTcpSocket->size(); qDebug() << in.device()->size(); qDebug() << "________________________________________________________"; QByteArray arr = in.device()->readAll(); qDebug() << arr.size(); qDebug() << "________________________________________________________"; pTcpSocket->disconnect(); return; }...
void IncomingDataHandler::run(){ qDebug() << "IncomingDataHandler thread started!"; QEventLoop evLoop; pTcpSocket = new QTcpSocket; pTcpSocket->moveToThread(this); //pTcpSocket->setSocketDescriptor(socketDescriptor); //socketDescriptor = pTcpSocket->socketDescriptor(); connect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(readData())); connect(pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionError(QAbstractSocket::SocketError))); connect(pTcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected())); connect(this, SIGNAL(dataProcessed()), &evLoop, SLOT(quit())); //connect(pTcpSocket, SIGNAL(disconnected()), &evLoop, SLOT(quit())); while (running) { requestData(); evLoop.exec(); //qDebug() << "socket disconnected... requesting new data"; }}void IncomingDataHandler::requestData(){ // TODO: remove sleep. temporary solution. QThread::msleep(500); qDebug() << "requesting data from server..."; // connect to server pTcpSocket->connectToHost(ipAddress, 777);}