-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_server.cpp
160 lines (135 loc) · 4.44 KB
/
my_server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "my_server.h"
#include "ui_my_server.h"
#include <QKeyEvent>
My_Server::My_Server(QWidget *parent)
: QMainWindow(parent), ui(new Ui::My_Server)
{
ui->setupUi(this);
ui->Server_StackWidget->hide();
currentClient = nullptr;
myServer =nullptr;
clientConnections = new QHash<QString, bool>();
}
My_Server::~My_Server()
{
delete ui;
}
void My_Server::on_Create_New_Server_BTN_clicked()
{
ui->Server_StackWidget->show();
if (myServer != nullptr) {
myServer->close();
delete myServer;
clientConnections->clear();
}
myServer = new QTcpServer();
connect(myServer, SIGNAL(newConnection()), this, SLOT(handle_new_connection()));
ui->server_state->clear();
ui->spinBox->clear();
ui->Server_StackWidget->show();
ui->Server_StackWidget->setCurrentIndex(0);
ui->Text_shower->clear();
}
void My_Server::handle_new_connection()
{
qDebug() << "handle new connection";
QTcpSocket* socket = myServer->nextPendingConnection();
QString clientAddress = socket->peerAddress().toString();
if (clientConnections->contains(clientAddress)) {
return;
}
currentClient = socket;
clientConnections->insert(clientAddress, true);
ui->Curent_Client_comboBox->addItem(clientAddress);
clients.append(socket);
connect(socket, SIGNAL(readyRead()), this, SLOT(ReadyReadFromClient()));
connect(socket, SIGNAL(disconnected()), this, SLOT(Disconnected_FromServer()));
ui->Text_shower->append("New connection from: " + clientAddress);
ui->Server_StackWidget->setCurrentIndex(1);
}
void My_Server::ReadyReadFromClient()
{
qDebug() << "read from clints";
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if (socket == nullptr) return;
QString clientAddress = socket->peerAddress().toString();
QByteArray data = socket->readAll();
ui->Text_shower->append(clientAddress + " : " + data);
}
void My_Server::Disconnected_FromServer()
{
qDebug() << "disconnect from server";
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if (socket == nullptr) return;
QString clientAddress = socket->peerAddress().toString();
clientConnections->remove(clientAddress);
int index = ui->Curent_Client_comboBox->findText(clientAddress);
if(index != -1)
ui->Curent_Client_comboBox->removeItem(index);
clients.removeOne(socket);
socket->deleteLater();
ui->Text_shower->append("Disconnected from: " + clientAddress);
}
void My_Server::sendDataToClient()
{
QByteArray data = ui->message_LE->text().toUtf8();
if (data.isEmpty()) return;
if (currentClient == nullptr && clients.isEmpty()) {
return;
}
if (ui->send_to_all->isChecked())
{
qDebug() << "send data to all clients";
ui->Text_shower->append("send to all clients : " + data);
for (QTcpSocket* client : clients)
{
client->write(data);
}
}
else
{
if (currentClient != nullptr)
{
qDebug() << "send data to " + ui->Curent_Client_comboBox->currentText();
ui->Text_shower->append("send to " + ui->Curent_Client_comboBox->currentText() + " : " + data);
currentClient->write(data);
}
}
}
void My_Server::on_Send_BTN_clicked()
{
qDebug() << "send btn clicked ";
QString message = ui->message_LE->text();
if (message.isEmpty()) return;
currentClient = nullptr;
QString current_clinet_ip = ui->Curent_Client_comboBox->currentText();
for(auto c : clients)
{
QString clientAddress = c->peerAddress().toString();
if(clientAddress == current_clinet_ip)
{
currentClient = c;
sendDataToClient();
break;
}
}
ui->message_LE->clear();
ui->message_LE->setFocus();
}
void My_Server::on_go_to_clients_clicked()
{
ui->Server_StackWidget->setCurrentIndex(1);
}
void My_Server::on_listen_btn_clicked()
{
if (myServer->isListening()) {
myServer->close();
ui->server_state->setText("Server stopped");
} else {
if (myServer->listen(QHostAddress::Any, ui->spinBox->value())) {
ui->server_state->setText("Server is listening to clients");
} else {
ui->server_state->setText("Failed to start server");
}
}
}