PDA

View Full Version : QSqlDatabase Mysql timeout versus PortScanner



patrik08
21st May 2008, 14:09
Is here a other way to stop QSqlDatabase MYSQL driver after 300 ms second on not connect?

I write this simple PortScanner and run ok ...
but i suppose QSqlDatabase can handle this how?





/* ShemaConnect = mysql://user:pass@host:port/databasename */

/*
ScanPort *portcheck = new ScanPort();
connect(portcheck, SIGNAL(result(int,int,bool)), this, SLOT(serveracess(int,int,bool)));
portcheck->SetUrl(QUrl(ShemaConnect));
portcheck->start(QThread::HighPriority);
*/


class ScanPort : public QThread
{
Q_OBJECT
public:
void SetUrl( QUrl check_url , int maxtime = 300 , QObject * redirect = 0 )
{
url = check_url;
receiver = redirect;
Timeout = maxtime;
setTerminationEnabled(true);
}
protected:
void run()
{
QTime st = QTime::currentTime();
m_Socket = new QTcpSocket();
m_Socket->connectToHost(url.host(),url.port(),QIODevice::Rea dOnly);
if (m_Socket->waitForConnected(Timeout)) {
if (m_Socket->waitForReadyRead(Timeout)) {
qDebug() << "### connect ok port " << url.port();
QTextStream *t = new QTextStream( m_Socket );
t->setCodec(QTextCodec::codecForMib(106));
int loops = 0;
while (!t->atEnd()) {
loops++;
response = t->readLine();
qDebug() << loops << " line " << response;
}
emit result(url.port(),runtime(st),true);
quit();
} else {
m_Socket->close();
qDebug() << "### connect nooo on port " << url.port();
emit result(url.port(),runtime(st),false);
quit();
}
} else {
m_Socket->close();
qDebug() << "### connect nooo on port " << url.port();
emit result(url.port(),runtime(st),false);
quit();
}
}
int runtime( QTime go )
{
QTime now = QTime::currentTime();
return go.msecsTo ( now );
}
signals:
void result(int,int,bool); /* port , time , true = ok */
private:
QString response;
int Timeout;
QTcpSocket *m_Socket;
uint millisecond;
QUrl url;
QObject* receiver;
};

jacek
25th May 2008, 00:08
Unfortunately you can't do much with MySQL driver. See QSqlDatabase::setConnectOptions().