QT client to C server
-
Ofcourse you can use the qDebug() in GUI Application. Just include the header file.
Whatever error you get during the connection handleError Slot will be called and the error will get printed. -
Did you put the parentheses for qDebug? "qDebug":http://qt-project.org/doc/qt-4.8/qdebug.html#basic-use is a function.
-
Ok I fixed it and now it runs. But still nothing happens as I press login. I assume the problem is in the login button slot
@void Login::on_pushButton_clicked()
{pSocket = new QTcpSocket (this); connect (pSocket, SIGNAL(connected()), SLOT(handleConnected())); connect (pSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT (handleError(QAbstractSocket::SocketError))); pSocket->connectToHost("127.0.0.1", 9000);
}@
-
Are you sure it is getting called on click ?
Have you declared on_pushButton_clicked() as a SLOT ? -
Yes. I already tested it by opening my next dialog directly. And it works. Isn't there a specific in order in the calling of connect and connectToHost? Or maybe should I place the connectToHost in my handleConnected?
-
The order is correct. All the signals must be connected before calling "connectToHost":http://qt-project.org/doc/qt-5.0/qtnetwork/qabstractsocket.html#connectToHost
The signal connected() gets emitted when the connection is established.Apart from these,
Which OS are you using ?
Try changing the port number. It may be blocked by the firewall. -
I'm on windows with my client. However, the server is on a linux machine. I know i wrote 127.0.0.1 but I did that just to hide the real IP. I tried with ui->lineEdit->text() and ui->lineEdit_2->text().toInt() but still doesn't works.
The server is designed to listen to 9000 port. I changed the port and no result. After the client is connected, he should send a message to the server. The server should send back the message 'Hello message'. It's pretty straightforward, but I know it's functional since I got connected from a C client and successfully sent and recieved data. I don't think that the message thing should represent a problem, as I should be able to establish a connection anyway. The server uses select for client acceptance - don't know if it's a relevant info
-
Is it a public IP ? May be it is taking time to connect to the Server.
Try waiting for some time as the handleError() should return some error atleast like QAbstractSocket::SocketTimeoutError or QAbstractSocket::HostNotFoundError or another..Another way to test would be to connect to a server using diifferent program.
As a quick test i using the python server, for e.g you can start a python HTTP server like this,
@
python -m SimpleHTTPServer
@or
@
python3.3 -m http.server
@By default it listens to port 8000.
So you can try connecting the client to python server with port 8000.
-
I started a python HTTP server as you said. The server was serving HTTP on 0.0.0.0 port 8000. When I completed the fields of my client with 0.0.0.0 and 8000, i got QAbstractSocket::NetworkError in the bottom side of the screen.
If i completed with the IP i knew and the port 8000, still nothing as I press login
It is public but I just connected to the server with a pre-made C client via putty. The server works, it transmited the message back to the C client
-
Can you post your complete client code here ?
-
Sure.
The login.h
@#ifndef LOGIN_H
#define LOGIN_H#include <QMainWindow>
#include <QtNetwork>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QDebug>namespace Ui {
class Login;
}class Login : public QMainWindow
{
Q_OBJECTpublic:
explicit Login(QWidget *parent = 0);
~Login();
private slots:
void on_pushButton_clicked();
void on_ExitLogin_clicked();
void handleConnected();
void handleError(QAbstractSocket::SocketError);
private:
Ui::Login *ui;
QTcpSocket *pSocket;};
#endif // LOGIN_H
@The login.cpp
@#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include "menu.h"
#include <QtNetwork>
#include <QLineEdit>
#include <QTcpSocket>
#include <QTcpServer>
#include <QAbstractSocket>
#include <QDebug>using namespace std;
Login::Login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
}Login::~Login()
{
delete ui;
}void Login::on_pushButton_clicked()
{
pSocket = new QTcpSocket (this);
connect (pSocket, SIGNAL(connected()), SLOT(handleConnected()));
connect (pSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT (handleError(QAbstractSocket::SocketError)));
pSocket->connectToHost(ui->lineEdit->text(),ui->lineEdit_2->text().toInt());}
void Login::on_ExitLogin_clicked()
{
QCoreApplication::instance()->exit();
}void Login::handleConnected()
{
Menu mMenu;
mMenu.setModal(true);
mMenu.exec();
}
void Login::handleError(QAbstractSocket::SocketError e)
{
qDebug() << e;
}
@The main.cpp
@#include "login.h"
#include <QApplication>
#include <QtNetwork>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Login w;
w.show();
return a.exec();
}
@I have some other classes like football, tennis etc since this should be a sports app. However, I have almost no code in them since I just designed them with drag and drop
-
Anyway it seemed like the client was functional and I would like to thank you for your interest. However, I have another problem now and I'd be glad if you could help. My app offers a menu after I pass the login part. There are a lot of options in the menu. I want to communicate with the server via the same socket used at the login part. My pSocket is declared in the Login class and I have no idea how to make all my other functions in other classes to use it.