Hi, I have read the suggested examples; this is the partial code.
In the main program I have added the following lines:
if (!tcpServer->listen()) {
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}
port = tcpServer->serverPort();
tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, tr("Server"),
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}
port = tcpServer->serverPort();
To copy to clipboard, switch view to plain text mode
I call the updater this way:
args << port.toString();
myProcess->startDetached(program, args);
QString program = "updater";
QStringList args;
args << port.toString();
myProcess = new QProcess(this);
myProcess->startDetached(program, args);
To copy to clipboard, switch view to plain text mode
In the updater I added:
tcpSocket->connectToHost("localhost", port.toInt());
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost("localhost", port.toInt());
To copy to clipboard, switch view to plain text mode
and send the quit command this way:
void Updater::sendQuit()
{
out << (quint16)0;
out << "quit";
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
tcpSocket->disconnectFromHost();
}
void Updater::sendQuit()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << "quit";
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
tcpSocket->disconnectFromHost();
}
To copy to clipboard, switch view to plain text mode
Now, how the server reads the incoming data? In the fortune client/server example the server has the following connection:
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
To copy to clipboard, switch view to plain text mode
and the function sendFortune() only sends data. Besides, the client reads data but uses as i/o device tcpSocket so perhaps I can't copy and paste its readFortune() code in main program.
Regards
Bookmarks