Unsolved Basic Problem with TCP connection
-
Hello, I'm doing a TCP connection between two PC on the same lan (one with 192.168.0.1 and the other with 192.168.0.2) but as I want to make it generic because I'm doing it for school so I use a lineEdit to get the pc's IP. The code is:
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTcpServer> #include <QTcpSocket> #include <QHostAddress> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); void new_connection(); void read(); private: Ui::MainWindow *ui; QHostAddress *direcction; QTcpServer *server; QTcpSocket *client; }; #endif // MAINWINDOW_H
Like you can see I use one QTcpServer and one QTcpSocket on the Server aplication which runs on one of the two pc, in the other I just make a QTcpSocket.
MainWindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); server = new QTcpServer(this); server ->setMaxPendingConnections(1); client = new QTcpSocket(); connect(server, SIGNAL(newConnection()),this, SLOT(new_connection())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { direcction=new QHostAddress(ui->IPinput->text().toInt()); //IPinput is a lineEdit server->listen(*direcction, 0); } void MainWindow::new_connection() { connect(client, SIGNAL(readyRead()), this, SLOT(read())); } void MainWindow::read() { QByteArray buffer; buffer.resize(cliente->bytesAvailable()); client->read(buffer.data(), buffer.size()); ui->textEdit->append(QString(buffer)); }
What do you thinks? Why it do not work?
The client application is
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtNetwork> #include <QTcpSocket> #include <QHostAddress> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_makeconnection_clicked(); void on_pushButton_clicked(); void read(); private: Ui::MainWindow *ui; QHostAddress *direcction; QTcpSocket *server; QString IP; }; #endif // MAINWINDOW_H
and Mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_hacerConexion_clicked() { IP= ui->IPinput->text(); direcction=new QHostAddress(IP); server = new QTcpSocket(this); server->connectToHost(IP,0, QIODevice::ReadWrite); connect(server, SIGNAL(readyRead()),this, SLOT(read())); } void MainWindow::on_pushButton_clicked() { QString data; data= ui->DataToSend->text(); server->write(ui->DataToSend->text().toLatin1().data(), ui->DataToSend->text().size()); ui->DataToSend->clear(); } void MainWindow::read() { QByteArray buffer; buffer.resize(servidor->bytesAvailable()); servidor->read(buffer.data(), buffer.size()); ui->label->setText(QString(buffer)); }
[Edit: Added code tags ~kshegunov]
-
Hi,
There might be several things. The first one being your QHostAddress that is invalid. You're converting the content of IPinput to an int but don't even check that you get a valid value.
Also note that you are leaking memory. You never delete
direcction
. There's no real need to allocate it on the heap.I'd recommend taking a look at Qt's networking examples before going further.
-
thanks!. So for example on the line:
client->connectToHost("10.0.0.3", 0, QIODevice::WriteOnly);
the QString line "10.0.0.3" is the server's ip but it's an local ip. I means it's the ip which the home's router dhcp server assigned to the local network. It's okay the sentence?
because what I do on the server it's:
connect (servidor,SIGNAL(newConnection()),this,SLOT(new_connection()));
and
void MainWindow::new_connection()
{
ui->state->setText("there is connection");
connect(cliente, SIGNAL(readyRead()), this, SLOT(read()));
}but on the label state I never get the message "there is connection".
-
You should add error checking to your code. You're not doing any so you're missing all information from them that could give you hints at what is not working or going wrong.
-
thanks. but what do you means with error checking? there is a special class for it? or it's a QTcpServer's method
-
Please, take the time to look at the class's documentation. You have e.g. the QTcpSocke::error signal.
On a side note, naming your QTcpSocket object server is quite confusing since there's also the QTcpServer class.