View Full Version : simple test connection
dousdou
4th March 2011, 09:29
i like to help me to do a simple test connexion (i have a button and i like whene i cliced it take the adresse ip and do a test if it's ok it will show me "host is connected" else "host is not connected" .
so can you help me and give me the code sorce of this app plz i realy need it
unit
4th March 2011, 10:30
Do you want to have button, textedit for IP input and then try connect to host with this IP?
dousdou
4th March 2011, 10:33
yes this is it
unit
4th March 2011, 10:54
Ok. simple example tcp connect to 80 port.
void MainWindow::on_pushButton_clicked()
{
ui->label->setText("");
connect(mysocket,SIGNAL(connected()),this,SLOT(on_ connected()));
connect(mysocket,SIGNAL(error(QAbstractSocket::Soc ketError)),this,SLOT(on_error(QAbstractSocket::Soc ketError)));
mysocket->connectToHost(ui->lineEdit->text(), 80);
}
void MainWindow::on_error(QAbstractSocket::SocketError socketError)
{
ui->label->setText("Not connected " + QString::number(socketError));
}
void MainWindow::on_connected()
{
ui->label->setText("It's ok. Connected");
mysocket->disconnectFromHost();
}
dousdou
4th March 2011, 11:03
realy realy thx but i have a erreur:
it told me:
QAbstractSocket::SocketError ==> is not a type name
what i sould do it ?
unit
4th March 2011, 11:06
Xm, try qthelp://com.trolltech.qt.472/qdoc/qabstractsocket.html
QAbstractSocket::SocketError is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType().
And QT demo example qthelp://com.trolltech.qt.472/qdoc/network-fortuneclient.html
dousdou
4th March 2011, 12:11
he told me now:
error: ‘myscoket’ was not declared in this scope
so what i should do ?
unit
4th March 2011, 20:54
Do you try attach that i do for you?
mysocket is pointer to QTcpSocket class
dousdou
7th March 2011, 13:21
this time i just like when we push the button he will go to fichier .txt and get the @ ip ligne by ligne and it will do the test
i'm realy amateur in prog so plz help me :)
void MainWindow::on_pushButton_clicked()
{
QFile fichier("/home/dousdou/idee.txt");
fichier.open(QIODevice::ReadOnly);
QString contenu = fichier.readAll();
fichier.close();
connect(mysocket,SIGNAL(connected()),this,SLOT(on_ connected()));
connect(mysocket,SIGNAL(error(QAbstractSocket::Soc ketError)),this,SLOT(on_error(QAbstractSocket::Soc ketError)));
mysocket->connectToHost(contenu, 22);
if (mysocket->waitForConnected(1000))
qDebug("Connected!");
};
but it doesn't work so where is the prb ?
unit
7th March 2011, 13:53
Show /home/dousdou/idee.txt file or attach it, plz
dousdou
7th March 2011, 15:00
it's a file text
unit
7th March 2011, 15:20
Oh. idee.txt contain only one line with IP? Or several?
For several line try some code:
QStringList iplist;
while (!file.atEnd())
{
iplist.append(file.readLine());
}
for(int i=0;i<iplist.count();++i)
{
...
mysocket->connectToHost(iplist.at(i), 22);
...
}
So, you try find not firewalled SSH. it's dirty job.
Ðre you then ask as to help with software for password selection?
dousdou
7th March 2011, 15:43
i push the button bute nothing is work :((((((((((((((
void MainWindow::on_pushButton_clicked()
{
QFile file("/home/dousdou/idee.txt");
QStringList iplist;
while (!file.atEnd())
{
iplist.append(file.readLine());
}
for(int i=0;i<iplist.count();++i)
{
connect(mysocket,SIGNAL(connected()),this,SLOT(on_ connected()));
connect(mysocket,SIGNAL(error(QAbstractSocket::Soc ketError)),this,SLOT(on_error(QAbstractSocket::Soc ketError)));
mysocket->connectToHost(iplist.at(i), 22);
if (mysocket->waitForConnected(1000))
qDebug("Connected!");
}
unit
7th March 2011, 15:55
You don't need use signal and slot if use waitForConnected
void MainWindow::on_pushButton_clicked()
{
QFile file("/home/dousdou/idee.txt");
file.open(QIODevice::ReadOnly);
QStringList iplist;
while (!file.atEnd())
{
iplist.append(file.readLine());
}
for(int i=0;i<iplist.count();++i)
{
mysocket->connectToHost(iplist.at(i), 22);
if (mysocket->waitForConnected(1000))
qDebug("Connected!");
else qDebug("Error!");
}
}
dousdou
8th March 2011, 08:29
I have done what you told me to do but also it doesn't work i know it a simple application and i realy try maney thing but always the some prb it doesn't work so when i execute your code this is what he told me:
Démarrage de /home/dousdou/test2-build-desktop/test2...
QMetaObject::connectSlotsByName: No matching signal for on_connected()
QMetaObject::connectSlotsByName: No matching signal for on_error(QAbstractSocket::SocketError)
Error!
Error!
Error!
Error!
unit
8th March 2011, 09:26
You try to do copy/paste and change part of code without thinking about it. You should read some manuals and src of ready code, and learn what code is do. Try new attach with full code.
I have next out:
Cann't connect to ??10.110.10.22 with error Host not found
Connected to 10.110.10.23
Connected to 10.110.10.9
Cann't connect to 10.110.10.105 with error Socket operation timed out
Cann't connect to 10.110.10.86 with error Socket operation timed out
dousdou
9th March 2011, 13:55
i just modify the code like this :
MainWindow::~MainWindow()
{
delete ui;
delete mysocket;
}
void MainWindow::on_pushButton_clicked()
{
QFile ipfile("/home/dousdou/tech-ip.txt");
if(!ipfile.open(QIODevice::ReadOnly))
{
qDebug("Can't open file");
return;
}
QStringList iplist;
while (!ipfile.atEnd())
{
iplist.append(ipfile.readLine());
}
ipfile.close();
for(int i=0;i<iplist.count();++i)
{
connect(mysocket,SIGNAL(connected()),this,SLOT(on_ connected()));
connect(mysocket,SIGNAL(error(QAbstractSocket::Soc ketError)),this,SLOT(on_error(QAbstractSocket::Soc ketError)));
mysocket->connectToHost(iplist.at(i), 22);
}
}
void MainWindow::on_error(QAbstractSocket::SocketError socketError)
{
QMessageBox msgBox;
msgBox.setText("Not connected " + QString::number(socketError));
msgBox.exec();
}
void MainWindow::on_connected()
{
QMessageBox msgBox;
msgBox.setText("It's ok. Connected");
msgBox.exec();
mysocket->disconnectFromHost();
}
and he show me this error:
QMetaObject::connectSlotsByName: No matching signal for on_connected()
QMetaObject::connectSlotsByName: No matching signal for on_error(QAbstractSocket::SocketError)
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "10.20.24.157
"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.1.5
"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "10.20.24.155
"
where is the rpb hier i know that's i'm not good in devlopment but plz i need your help i can work lonly and you are my last chance :)
unit
9th March 2011, 14:22
are you kidding me?
Don't modify my CODE now. Only change path to file and try! Or tell me all your task and i try give you complete code. My Skype is unit_by
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.