struct head{ quint32 size; quint32 tit; char name[1024];}
C++ (Qt)#pragma pack(1)struct head{ quint32 size; quint32 tit; char name[1024];}#pragma pack()
C++ (Qt)file.write(&theHead, sizeof(head));
head objHead;QByteArray arrBl;arrBl.append((const char*)&objHead,sizeof(objHead));m_pTcpSocket->write(arrBl);
QString str;//......................................strcpy(objHead.name, str.toLocal8Bit().data());
void FormCl::slotSendToServerFile(){ QFile file(wayFile); QByteArray arrBlock, arrBlockText, arrBlockNameFile; headl objHead; objHead.titMsg = 2; int pos, k, i; for(int i = wayFile.size()-1, j = 0; wayFile.at(i) != '/'; i--, j++) { pos = j; objHead.nameFile[i] = NULL; } for(i = wayFile.size()-1 - pos, k = 0; k < pos + 1; i++, k++) objHead.nameFile[k] = wayFile.at(i).toAscii(); // чтение из файла if(file.open(QIODevice::ReadOnly)) { objHead.sizeData = file.size(); arrBlockText = file.read(objHead.sizeData); arrBlock.append((const char*)&objHead, sizeof(objHead)); arrBlock.append(arrBlockText); m_pTcpSocket->write(arrBlock); file.close(); }}
...... while(true) { if(tcpSocket->state() == QAbstractSocket::ClosingState) break; if(tcpSocket->waitForReadyRead(3000)) // bytesAvailable() { if(!flag) // false { // читаем переданную стуктуру! retrRead = tcpSocket->read((char*)&objHead,sizeof(objHead)); if( retrRead == -1) break; if( retrRead == 0) break; } switch(objHead.titMsg) { case 1: // передается сообщение { ..... break; } case 2: // передается файл { // запись в файл QFile file(objHead.nameFile); if(file.open(QIODevice::WriteOnly)) { if(tcpSocket->bytesAvailable() != objHead.sizeData) { flag = true; continue; } arrBlockText = tcpSocket->read(objHead.sizeData); file.write(arrBlockText); emit MessageClient(tcpSocket, "<b>\nSize file: </b>" + QString().setNum(objHead.sizeData) + " Byte", 2); emit msgToGUI(QTime::currentTime().toString()+" <b>File is sent</b>",2); flag = false; file.close(); } else { if(tcpSocket->bytesAvailable() != objHead.sizeData) { flag = true; continue; } tcpSocket->read(objHead.sizeData); flag = false; msgToGUI("<b>Error</b> of the determination of the file!",4); } break; } default: { continue; } } // switch() } // waitForReadyRead() } // while()