PDA

View Full Version : problem with QTcpSocket



SuperSonik
30th January 2007, 13:42
Hi,
I've a problem with using the QTcpSocket.
I ty to write a programm to interact with a database over tcp.
I have to use this way because the database is not normaly supported by Qt (it's TinySQL database).

Here are my code so far:


void connectToServer(void){
pushButton_abfragen->setText(tr("Abfragen 2......"));
tcpsocket.connectToHost(lineEdit_ipgypsy->text(),5050);
pushButton_abfragen->setText(tr("Abfragen ......"));
pushButton_upload->setEnabled(false);
pushButton_abfragen->setEnabled(false);

};//connectToServer

void sendRequest(void){

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_2);
QString abfrage("SELECT * FROM Datum; \r\n");


out << abfrage.toAscii();
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
tcpsocket.write(block);
pushButton_abfragen->setText(tcpsocket.errorString());

};//sendRequest

void getData(void){
pushButton_abfragen->setText(tr("Abfragen 2......"));
//pushButton_abfragen->setText(tcpsocket.errorString()+" --1");

QFile confdatei("testdata.txt");
confdatei.open(QIODevice::WriteOnly);
QDataStream conf(&confdatei);
QByteArray ip,wert,datum;
conf.setVersion(QDataStream::Qt_4_2);

//pushButton_abfragen->setText(tcpsocket.errorString()+" --2");

QDataStream in(&tcpsocket);
in.setVersion(QDataStream::Qt_4_2);

//pushButton_abfragen->setText(tcpsocket.errorString()+" --3");

forever{

if(nextBlockSize == 0){
if(tcpsocket.bytesAvailable() < sizeof(quint16)){ break; }
in >> nextBlockSize;
}
if(nextBlockSize == 0xFFFF){
closeConnection();
break;
}
if(tcpsocket.bytesAvailable() < nextBlockSize){ break;}

conf << in;//ip << wert << datum;


nextBlockSize = 0;
}





};//getData

void closeConnection(void){

tcpsocket.close();
pushButton_abfragen->setText(tr("Daten abfragen"));
pushButton_upload->setEnabled(true);
pushButton_abfragen->setEnabled(true);

};//closeConnection

void displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
pushButton_abfragen->setText(tr("Daten abfragen"));
pushButton_upload->setEnabled(true);
pushButton_abfragen->setEnabled(true);
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
pushButton_abfragen->setText(tr("Daten abfragen"));
pushButton_upload->setEnabled(true);
pushButton_abfragen->setEnabled(true);
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(tcpsocket.errorString()));
pushButton_abfragen->setText(tr("Daten abfragen"));
pushButton_upload->setEnabled(true);
pushButton_abfragen->setEnabled(true);
}


}



tcpsocket and nextBlockSize are member variables. And all connect - calls are made.
I think the problem is the line:


QString abfrage("SELECT * FROM Datum; \r\n");

If I send this line the readyRead() signal is emitted, and then direkt the disconnected() signal in response. I never get a error by the displayError slot.
I thought the prolem is \r\n and I try to use \0 and single \n and so on.
But it is all the same.
So what's wrong? At the moment I have no idea what I can try out.
Greetings,
SuperSonik

wysota
30th January 2007, 13:49
Maybe the database causes the disconnection? My guess is that you shouldn't use QDataStream, it does something more than just stream the data. Try using QTextStream instead.

Anyway, maybe you should write your own database driver for that database?

SuperSonik
30th January 2007, 14:00
Hi,
first thanks for your answer. You are the first in 3 forums who gives me a hint :D in 4 days!
I try to use QTextStream and write what happens. -> Must I chance QByteArray ?
Thanks,
SuperSonik

SuperSonik
30th January 2007, 14:21
Hi,
I once again :) . How can I write with QTextStream on QTcpSocket?:confused:
Can someone give me an example?
Greetings,

SuperSonik

wysota
30th January 2007, 14:53
You are the first in 3 forums who gives me a hint :D in 4 days!
As you see we have pretty good answer times here.

I try to use QTextStream and write what happens. -> Must I chance QByteArray ?

Open the text stream to the byte array and stream the QString contents through. You should end up with a byte array which you can then pass to QTcpSocket::write().

danadam
30th January 2007, 15:24
void sendRequest(void) {
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_2);
QString abfrage("SELECT * FROM Datum; \r\n");

out << abfrage.toAscii();
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
tcpsocket.write(block);
pushButton_abfragen->setText(tcpsocket.errorString());
};//sendRequest

I'm not sure if this code is really what you wanted to do. toAscii() method converts QString to QByteArray. QByteArray written to QDataStream is represented (http://doc.trolltech.com/4.2/datastreamformat.html) as array size (quint32) followed by the array bytes. So in block variable you have something like this:
[x][x][x][x][S][E][L][E][C][T][...] and then you rewind to position 0 and overwrite first two bytes:
[y][y][x][x][S][E][L][E][C][T][...] Is this realy what you wanted?

SuperSonik
31st January 2007, 09:51
Hi,
I'm not sure. I read a Qt book, and there I read I have to write the lenght of my message first
in my tcp package as a qint64.
I took the code out of the book and chance a few things.
In real I only want to send the string as I write it in the QString. -> exactly this String
I thought to write the lenght before the message is something I have to do because of tcp.
Greetings,
SuperSonik

wysota
31st January 2007, 14:20
Take a look at the docs, you'll notice the write() method that takes a byte array as its argument (it's probably defined in QIODevice class).

SuperSonik
31st January 2007, 16:00
Ok,
I have found my problem. I think my programm is working for a while.
The error message was wrong. If I call tcpsocket.errorString() and there is no error the funktion returns "Unknown error".
Now it's working. Thank you to all.
Greetings,
SuperSonik