PDA

View Full Version : QTcpSocekt dosent recive msg



Zergi
11th January 2008, 17:15
Hi

I have very strange problem, QTcpSocket recive msg when i write something like that
tcpSocket->write("P\n"); but if i write something like that

tcpSocket->write("P"+t.toLatin1()); it dosent work
where t is QString

Sending function(server)

void server::Twyslijdane()
{
QString t;
QSettings settings("Users.ini",QSettings::IniFormat);
keys = settings.allKeys();
for (int i = 0; i < keys.size(); i = i+2) {
t = keys.at(i);
t.remove(QString("/Login"), Qt::CaseInsensitive);
t.remove(QString("/pass"), Qt::CaseInsensitive);


connection->write("D"+t.toLatin1());

}
}

reciving function

{
qint64 bytes = buffer->write(tcpSocket->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())
{
line = buffer->readLine();
if(line[0] == 'D'){
line.remove(0,1);
dodajnicki(line);
}

}



}

When i m sending tcpSocket->write("P"+t.toLatin1()); program dosent use
this part of code
while (buffer->canReadLine())
{
line = buffer->readLine();
if(line[0] == 'D'){
line.remove(0,1);
dodajnicki(line);
}

Best Regards

jacek
12th January 2008, 19:19
What happens if you send "P" + t.toLatin1() + "\n"?

bender86
12th January 2008, 19:42
tcpSocket->write("P\n"); // "P\n" is a const char *
tcpSocket->write("P"+t.toLatin1()); // "P" + t.toLatin1() is a QString
Does QIODevice::canReadLine() expect to read char* data? If so, you can use
tcpSocket->write(QString("P" + t).toLatin1().data());

jacek
12th January 2008, 19:56
"P" + t.toLatin1() is a QString
That's a QByteArray, not QString.

Fastman
14th January 2008, 08:08
May be:


QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);

block = NULL;
block.append("Some Msg");
tcpSocket->write(block);

bender86
14th January 2008, 11:24
That's a QByteArray, not QString.
Right. So the problem is not that. Try how suggested by jacek.