PDA

View Full Version : QTcpSoket and QTcpServer problem.



v3n0w
12th June 2007, 21:00
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
public slots:

void Conectar()
{
socket->abort();
socket->connectToHost("Localhost", 1050);
}

private:
QTcpSocket *socket;
};

class MyQTextEdit : public QTextEdit
{
Q_OBJECT
QLineEdit * textoEnvia;

public:
MyQTextEdit(QWidget *parent, QLineEdit* tE) : QTextEdit(parent), textoEnvia(tE){

}

public slots:

void EnviaMsg()
{
char * Data = "Data";
append(textoEnvia->text());
textoEnvia->clear();

}
};


#include "main.moc"

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(600, 300);

socket = new QTcpSocket(this);

QWidget * wLayout = new QWidget(this);
QLineEdit * textoEnvia = new QLineEdit(wLayout);
QPushButton *enviar = new QPushButton(tr("Enviar"), wLayout);
QPushButton *conectar = new QPushButton(tr("Conectar"), wLayout);
MyQTextEdit *output = new MyQTextEdit(this, textoEnvia);
output->setMaximumHeight(200);

QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *lLayout = new QHBoxLayout;

lLayout->addWidget(textoEnvia);
lLayout->addWidget(enviar);
lLayout->addWidget(conectar);
wLayout->setLayout(lLayout);

layout->addWidget(output);
layout->addWidget(wLayout);

setLayout(layout);

textoEnvia->setText("Digite aqui o texto...");

connect(enviar, SIGNAL(clicked()), output, SLOT(EnviaMsg()));
connect(conectar, SIGNAL(clicked()), this, SLOT(Conectar()));
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

This is the Client, it send a msg:"Caio", when the buttom "enviar" is clicked.

And Connects with the server when the buttom "conectar" is cliked.


#include "server.h"

Server::Server(QWidget *parent)
: QDialog(parent)
{
statusLabel = new QLabel(this);

tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, tr("Fortune Server"),
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}

statusLabel->setText(tr("The server is running on port %1.\n"
"Run the Fortune Client example now.")
.arg(tcpServer->serverPort()));

connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));

setWindowTitle(tr("Fortune Server"));
}

void Server::newClient()
{
statusLabel->setText(tr("Connectou"));
clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(Leia()));

}

void Server::Leia()
{
char * Data;
clientConnection->read(Data,256);
statusLabel->setText(Data);
statusLabel->setText("Caiow");
}

And here's the server.

But the problem is simple, this connect never executes: connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));

Why?

I execute the server, and after the client, the same Address and the same port. :confused:

wysota
13th June 2007, 01:02
Do you have the Q_OBJECT macro in the Server class?

v3n0w
13th June 2007, 01:18
Do you have the Q_OBJECT macro in the Server class?

I do. Its the same code of the fortune server example.

v3n0w
13th June 2007, 02:55
Here's the server.h:


#ifndef SERVER_H
#define SERVER_H

#includes ...

class QLabel;
class QPushButton;
class QTcpServer;

class Server : public QDialog
{
Q_OBJECT

public:
Server(QWidget *parent = 0);

public slots:
void newClient();
void Leia();

private:
QLabel *statusLabel;
QTcpServer *tcpServer;
QTcpSocket *clientConnection;
};

#endif

wysota
13th June 2007, 06:44
And what about the "main()" of the server? Could we see it as well? BTW. Your Leia method is incorrect. You have to have a buffer to read into, not only a pointer.

v3n0w
13th June 2007, 22:03
And what about the "main()" of the server? Could we see it as well? BTW. Your Leia method is incorrect. You have to have a buffer to read into, not only a pointer.

Sure :
#include <stdlib.h>

#include "server.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Server server;
server.show();
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
return server.exec();
}

I know about the Leia method, but isn't the problem right now.

Thanks!

[EDIT] I change the server.exec() to app.exec(), but nothing hapens.

v3n0w
14th June 2007, 05:02
SOLVED!!

And I can't edit my posts...

wysota
14th June 2007, 09:26
How did you solve it?

spiderpig
22nd October 2007, 16:28
I am having the same problem, i.e. the newConnection signal is never emitted from QTcpServer. Can you please share with us the solution to the problem in your example code?


SOLVED!!

And I can't edit my posts...

wysota
22nd October 2007, 18:20
Do you use threads? Does the connection really happen, regardless of emited signals? Do you get any warnings on the console?