problem with QByteArray data !
i am writing a chat application using TCP.
i found a simple exemple of text chat here : http://www.qtcentre.org/wiki/index.p...le=Simple_chat.
i edited its code so that it can send QByteArray data instead of Qstring like it did in the beginning,but after editing, Server didn't send back message to clients.
help me fix my code.
a part of simplechatclient.cpp :
Code:
void SimpleChatClient::sendMessage()
{
if(socket == NULL) return;
QString data
= "<" + nick
->text
().
toLatin1() + "> " + message
->text
().
toLatin1() + "\n";
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
socket->write(block);
socket->flush();
message->clear();
"Client send ok." );
}
void SimpleChatClient::receiveMessage()
{
if (socket == NULL) return;
//in.setVersion( in.version() ); // set to the current Qt version instead
if(blockSize == 0) {
if (socket->bytesAvailable() < (int)sizeof(quint32)) return;
in >> blockSize;
}
if(socket->bytesAvailable() < blockSize) return;
in >> msgString;
in >> msgData;
chat->append(msgData);
blockSize = 0;
if(socket->bytesAvailable() > 0) receiveMessage();
}
a part of simplechatsever.cpp :
Code:
void SimpleChatServer::receiveMessage()
{
QTcpSocket* socket
= static_cast<QTcpSocket
*>
(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;
emit onMessage( msgString, msgData );
blockSize = 0;
if (socket->bytesAvailable() > 0) receiveMessage();
}
void SimpleChatServer
::onMessage(const QString &msg,
const QString &data
) {
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
{
connection->write(block);
connection->flush();
}
}
Re: problem with QByteArray data !
Quote:
i edited its code so that it can send QByteArray data instead of Qstring like it did in the beginning,but after editing, Server didn't send back message to clients.
I hope you don't expect us to go to the other thread, and compare the code there with your code.
Please post the changes you made so that we can see them in relation to the original.
Re: problem with QByteArray data !
i' sorry .
this is the original code :
a part of simplechatclient.cpp :
Code:
void SimpleChatClient::sendMessage()
{
// "<nick> message\n"
socket->write("<" + nick->text().toLatin1() + "> " + message->text().toLatin1() + "\n");
message->clear();
}
void SimpleChatClient::receiveMessage()
{
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine())
{
chat->append(line.simplified());
}
}
a part of simplechatsever.cpp :
Code:
void SimpleChatServer::receiveMessage()
{
QTcpSocket* socket
= static_cast<QTcpSocket
*>
(sender
());
QBuffer* buffer
= buffers.
value(socket
);
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine())
{
{
connection->write(line);
}
}
}
I had fix only at these methods And declare quint32 blockSize in the header file.
Re: problem with QByteArray data !
Did you not try to debug your code before posting here?
Eg. does sendMessage transmit a valid message?
Which lines of receiveMessage are actually executed?
Re: problem with QByteArray data !
i try to debug program and i get some error on "if (socket->bytesAvailable() < blockSize) return; ", i delete it from my source code then sendMessage and receiveMessage run on both server and client, but messenge data not receive, i have just known a little about it, but not mastered yet!
help me ,i really need it :-(( thank in advance .
Re: problem with QByteArray data !
UP ... help me :-(( i verry need it :-S
Re: problem with QByteArray data !
Check what blockSize contains after you stream data to it from the data stream.
By the way, I think the code for the chat needs to be reviewed. Creating a data stream in a slot connected to readyRead() is really a bad idea.