connect(tcpSocket, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
rowcnt=1;
if (!tcpServer->listen()) {
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()));
}
ControlPanel::~ControlPanel()
{
}
void ControlPanel::sendFortune()
{
out << (quint16)0;
out << tempdata;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
QTcpSocket *clientConnection
= tcpServer
->nextPendingConnection
();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->write(block);
//clientConnection->disconnectFromHost();
qDebug()<<tempdata;
tempdata.clear();
}
void ControlPanel::extract()
{
// Retrieving data from db and appending it to tempdata and then calling the function sendFortune()
sendFortune();
}
void ControlPanel::on_pushButton_clicked()
{
// I wanted to send data every Second so i am calling extract() every second.
connect(timerdraw, SIGNAL(timeout()), this, SLOT(extract()));
timerdraw->start(1000);
}
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
rowcnt=1;
tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, tr("Data 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()));
}
ControlPanel::~ControlPanel()
{
}
void ControlPanel::sendFortune()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << tempdata;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->write(block);
//clientConnection->disconnectFromHost();
qDebug()<<tempdata;
tempdata.clear();
}
void ControlPanel::extract()
{
// Retrieving data from db and appending it to tempdata and then calling the function sendFortune()
sendFortune();
}
void ControlPanel::on_pushButton_clicked()
{
// I wanted to send data every Second so i am calling extract() every second.
connect(timerdraw, SIGNAL(timeout()), this, SLOT(extract()));
timerdraw->start(1000);
}
To copy to clipboard, switch view to plain text mode
This is the receiver side of the program.
setupUi(this);
connect(hostLineEdit,
SIGNAL(textChanged
(const QString &)),
this, SLOT(enablepushButton()));
connect(portLineEdit,
SIGNAL(textChanged
(const QString &)),
this, SLOT(enablepushButton()));
//connect(pushButton, SIGNAL(clicked()),
// this, SLOT(requestNewFortune()));
connect(pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
}
void TCP_DataReceiver::requestNewFortune()
{
pushButton->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
void TCP_DataReceiver::readFortune()
{
if (blockSize == 0)
{
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
in >> nextFortune;
// if (nextFortune == currentFortune)
// {
// QTimer::singleShot(0, this, SLOT(requestNewFortune()));
// return;
// }
currentFortune = nextFortune;
statusLabel->setText(currentFortune);
textBrowser->append(currentFortune);
currentFortune.clear();
pushButton->setEnabled(true);
}
setupUi(this);
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
tcpSocket = new QTcpSocket(this);
timerdraw = new QTimer(this);
timerreq = new QTimer(this);
connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enablepushButton()));
connect(portLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enablepushButton()));
//connect(pushButton, SIGNAL(clicked()),
// this, SLOT(requestNewFortune()));
connect(pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
void TCP_DataReceiver::requestNewFortune()
{
pushButton->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
void TCP_DataReceiver::readFortune()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0)
{
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
QString nextFortune;
in >> nextFortune;
// if (nextFortune == currentFortune)
// {
// QTimer::singleShot(0, this, SLOT(requestNewFortune()));
// return;
// }
currentFortune = nextFortune;
statusLabel->setText(currentFortune);
textBrowser->append(currentFortune);
currentFortune.clear();
pushButton->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
Bookmarks