network: 固定连接 localhost:33333
This commit is contained in:
parent
29b537296b
commit
afab1be637
|
@ -33,33 +33,7 @@ Client::Client(QWidget *parent)
|
|||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||
|
||||
hostCombo->setEditable(true);
|
||||
// find out name of this machine
|
||||
QString name = QHostInfo::localHostName();
|
||||
if (!name.isEmpty()) {
|
||||
hostCombo->addItem(name);
|
||||
QString domain = QHostInfo::localDomainName();
|
||||
if (!domain.isEmpty())
|
||||
hostCombo->addItem(name + QChar('.') + domain);
|
||||
}
|
||||
|
||||
if (name != QLatin1String("localhost"))
|
||||
hostCombo->addItem(QString("localhost"));
|
||||
|
||||
// find out IP addresses of this machine
|
||||
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
||||
|
||||
// add non-localhost addresses
|
||||
for (int i = 0; i < ipAddressesList.size(); ++i) {
|
||||
if (!ipAddressesList.at(i).isLoopback())
|
||||
hostCombo->addItem(ipAddressesList.at(i).toString());
|
||||
}
|
||||
|
||||
// add localhost addresses
|
||||
for (int i = 0; i < ipAddressesList.size(); ++i) {
|
||||
if (ipAddressesList.at(i).isLoopback())
|
||||
hostCombo->addItem(ipAddressesList.at(i).toString());
|
||||
}
|
||||
|
||||
hostCombo->addItem(QString("localhost"));
|
||||
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
||||
|
||||
auto hostLabel = new QLabel(tr("&Server name:"));
|
||||
|
@ -118,6 +92,7 @@ Client::Client(QWidget *parent)
|
|||
|
||||
setWindowTitle(QGuiApplication::applicationDisplayName());
|
||||
portLineEdit->setFocus();
|
||||
portLineEdit->setText("33333");
|
||||
|
||||
QNetworkConfigurationManager manager;
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ Server::Server(QWidget *parent)
|
|||
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||||
|
||||
QNetworkConfigurationManager manager;
|
||||
|
||||
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
|
||||
// Get saved network configuration
|
||||
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
|
||||
|
@ -101,6 +102,7 @@ void Server::sessionOpened()
|
|||
if (networkSession) {
|
||||
QNetworkConfiguration config = networkSession->configuration();
|
||||
QString id;
|
||||
|
||||
if (config.type() == QNetworkConfiguration::UserChoice)
|
||||
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
|
||||
else
|
||||
|
@ -113,7 +115,8 @@ void Server::sessionOpened()
|
|||
}
|
||||
|
||||
tcpServer = new QTcpServer(this);
|
||||
if (!tcpServer->listen()) {
|
||||
|
||||
if (!tcpServer->listen(QHostAddress::LocalHost, 33333)) {
|
||||
QMessageBox::critical(this, tr("Server"),
|
||||
tr("Unable to start the server: %1.")
|
||||
.arg(tcpServer->errorString()));
|
||||
|
@ -136,6 +139,7 @@ void Server::sessionOpened()
|
|||
// if we did not find one, use IPv4 localhost
|
||||
if (ipAddress.isEmpty())
|
||||
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
||||
|
||||
statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2")
|
||||
.arg(ipAddress).arg(tcpServer->serverPort()));
|
||||
}
|
||||
|
@ -150,6 +154,7 @@ void Server::sendAction()
|
|||
out << actions[QRandomGenerator::global()->bounded(actions.size())];
|
||||
|
||||
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
|
||||
|
||||
connect(clientConnection, &QAbstractSocket::disconnected,
|
||||
clientConnection, &QObject::deleteLater);
|
||||
|
||||
|
|
Loading…
Reference in New Issue