/
server.cpp
49 lines (40 loc) · 1.56 KB
/
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
#include "server.h"
#include <QDebug>
#include <QMessageBox>
Server::Server()
: QThread(), tcpServer(NULL)
{}
void Server::run() {
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any, 7890);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
exec();
}
void Server::acceptConnection() {
if(sockets.count() < 4) {
QTcpSocket * socket = tcpServer->nextPendingConnection();
emit error(QString("setting up new connection: %1").arg((int)socket));
sockets << socket;
connect(socket, SIGNAL(readyRead()), this, SLOT(processRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(deleteConnection()));
connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
socket->write(QString("ACCEPT\n").toAscii());
} else {
QTcpSocket * socket = tcpServer->nextPendingConnection();
emit error("To much connections");
socket->write(QString("GET LOST\n").toAscii());
connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
//socket->disconnectFromHost();
}
}
void Server::processRead() {
// gets sender of signal
QTcpSocket * sock = qobject_cast<QTcpSocket * >(this->sender());
emit error(QString("from: %1 :: %2").arg((int)sock).arg(QString(sock->readAll())));
}
void Server::deleteConnection() {
QTcpSocket * sock = qobject_cast<QTcpSocket * >(this->sender());
sockets.removeAll(sock);
emit error(QString("%1 is disconnecting.").arg((int)sock));
//if(sock) delete sock;
}