this is my code :
client side,when connected() occurs :
if(socket == NULL) return;
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
socket->write(block);
socket->flush();
if(socket == NULL) return;
if(socket->state() != QAbstractSocket::ConnectedState ) return;
QString msg = "NICK";
QVariant data = QVariant(ui->nick->text().toLatin1());
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_2);
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
socket->write(block);
socket->flush();
To copy to clipboard, switch view to plain text mode
site sever when receive a message from cilent :
MyTcpSocket* socket = static_cast<MyTcpSocket*>(sender());
if (socket == NULL) return;
if (blockSize == 0) {
if (socket->bytesAvailable() < (int)sizeof(quint32)) return;
in >> blockSize;
}
if (socket->bytesAvailable() < blockSize) return;
in >> msgString;
in >> msgData;
if(msgString != "NICK"){
onMessage( msgString, msgData );
}else{
socket->setGroup(1);
qDebug() << "value of group is : " << socket->getGroup();
socket->setClientUser(msgData.toString());
qDebug() << "ok";
}
blockSize = 0;
if (socket->bytesAvailable() > 0) receiveMessage();
MyTcpSocket* socket = static_cast<MyTcpSocket*>(sender());
if (socket == NULL) return;
if ( socket->state() != QAbstractSocket::ConnectedState ) return;
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_2);
if (blockSize == 0) {
if (socket->bytesAvailable() < (int)sizeof(quint32)) return;
in >> blockSize;
}
if (socket->bytesAvailable() < blockSize) return;
QString msgString;
QVariant msgData;
in >> msgString;
in >> msgData;
if(msgString != "NICK"){
onMessage( msgString, msgData );
}else{
socket->setGroup(1);
qDebug() << "value of group is : " << socket->getGroup();
socket->setClientUser(msgData.toString());
qDebug() << "ok";
}
blockSize = 0;
if (socket->bytesAvailable() > 0) receiveMessage();
To copy to clipboard, switch view to plain text mode
Application sever output :
value of group is : 1
D:\ProGram\QT\SimpleChat-build-desktop\SimpleChatServer\debug\SimpleChatServer.ex e exited with code -1073741819
Bookmarks