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){
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");
//pushButton_abfragen->setText(tcpsocket.errorString()+" --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
{
switch (socketError) {
break;
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;
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:
tr("The following error occurred: %1.")
.arg(tcpsocket.errorString()));
pushButton_abfragen->setText(tr("Daten abfragen"));
pushButton_upload->setEnabled(true);
pushButton_abfragen->setEnabled(true);
}
}
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);
}
}
To copy to clipboard, switch view to plain text mode
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");
QString abfrage("SELECT * FROM Datum; \r\n");
To copy to clipboard, switch view to plain text mode
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
Bookmarks