-
Notifications
You must be signed in to change notification settings - Fork 0
/
createconnectdialog.cpp
69 lines (62 loc) · 1.8 KB
/
createconnectdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "createconnectdialog.h"
#include "ui_createconnectdialog.h"
#include <QNetworkInterface>
createConnectDialog::createConnectDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::createConnectDialog)
{
ui->setupUi(this);
QString strIpAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
int nListSize = ipAddressesList.size();
for (int i = 0; i < nListSize; ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
strIpAddress = ipAddressesList.at(i).toString();
break;
}
}
if (strIpAddress.isEmpty())
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
ui->lineEdit->setText(strIpAddress);
}
createConnectDialog::~createConnectDialog()
{
delete ui;
if(listenSocket){
listenSocket->close();
}
}
QHostAddress createConnectDialog::getAddress()
{
return QHostAddress(ui->lineEdit->text());
}
quint16 createConnectDialog::getPort()
{
return ui->lineEdit_2->text().toUShort();
}
QTcpSocket *createConnectDialog::getReadWriteSocket()
{
return readWriteSocket;
}
void createConnectDialog::acceptConnection()
{
qDebug() << "accept";
readWriteSocket = listenSocket->nextPendingConnection();
listenSocket->close();
return QDialog::accept();
}
void createConnectDialog::accept()
{
//qDebug() << "accept";
if(listenSocket == nullptr){
listenSocket =new QTcpServer;
}
listenSocket->listen(QHostAddress::Any,getPort());
ui->lineEdit->setVisible(false);
ui->lineEdit_2->setVisible(false);
ui->label->setVisible(false);
ui->label_2->setVisible(false);
QObject::connect(listenSocket,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
}