Hello, everyone! I hope I’m not out of line by asking for this kind of help.
I’m trying to create a client/server application. The server is looking really good. I used the FortuneServer example to get it to start listening… But, I noticed that each time the server is ran, the port it listens on changes…
void MainWindow::beginListening(){
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
networkSession = new QNetworkSession(config, this);
//connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
addText("Opening network session.");
//statusLabel->setText(tr("Opening network session."));
networkSession->open();
} else {
sessionOpened();
}
}
void MainWindow::sessionOpened()
{
// Save the used configuration
if (networkSession) {
QNetworkConfiguration config = networkSession->configuration();
QString id;
if (config.type() == QNetworkConfiguration::UserChoice)
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
else
id = config.identifier();
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
settings.endGroup();
}
//! [0] //! [1]
tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, tr("Server"),
tr("Unable to start the server: %1.\n")
.arg(tcpServer->errorString()));
close();
return;
}
//! [0]
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
addText(tr("The server is running on\nIP: %1\nPort: %2")
.arg(ipAddress)
.arg(tcpServer->serverPort()));
//! [1]
}
There is the beginListening function, with which I shoved the configure code in (the constructor method from the FortuneServer example, pretty much) and then there’s the SessionOpened function that gets called afterwards. I’ve read, reread, and triple read the code and I can’t seem to figure out how it does whatever it does. I just know that it does. XD Now, to run a server that is to serve multiple clients, I can’t have the server’s port change every time it’s ran… Is there a way to get the server to listen on a specific port, every time?
And my second question is a little more broader… I’m used to 2D programming in VB using the Picture Box control and DirectX… Is there an equivalent method I could use with Qt? Obviously, I don’t want to use DirectX, so maybe OpenGL? Idk, I don’t need help as much as I need to be pushed in the right direction to achieve my goals.
Thanks for anyone who responds. :D
↧