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:
{
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: