void Server::acceptConnection()
{
//Allocate space for new connection
ClientSocket *client = new ClientSocket(this);
client = (ClientSocket*)server->nextPendingConnection();
if (!client)
QMessageBox::information(this, tr
("Error"), tr
("No client pointer allocated"));
//Seach for client in client list
int found = -1;
if (!clients.isEmpty()) {
for (int i = 0; i < clients.size(); ++i) {
if (clients.at(i)->peerAddress().toString() == client->peerAddress().toString())
found = i;
}
//If not found in array: Don't add duplicates ie. from same host
if (found == -1) {
clients.append(client);
++connectionCount;
updateConnectionLabel();
connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
clients.last()->setReadBufferSize(READ_BUFFER_MAX);
} else {
//If already connected, add to data client list: Add duplicates to data client array
if (connectedClients < MAX_DATA_CLIENTS) {
dataClients.append(client);
++connectedClients;
updateConnectionLabel();
connect(dataClients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
}
}
} else {
//Add to array
clients.append(client);
++connectionCount;
updateConnectionLabel();
connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
clients.last()->setReadBufferSize(READ_BUFFER_MAX);
clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
}
}
void Server::acceptConnection()
{
//Allocate space for new connection
ClientSocket *client = new ClientSocket(this);
client = (ClientSocket*)server->nextPendingConnection();
if (!client)
QMessageBox::information(this, tr("Error"), tr("No client pointer allocated"));
//Seach for client in client list
int found = -1;
if (!clients.isEmpty()) {
for (int i = 0; i < clients.size(); ++i) {
if (clients.at(i)->peerAddress().toString() == client->peerAddress().toString())
found = i;
}
//If not found in array: Don't add duplicates ie. from same host
if (found == -1) {
clients.append(client);
++connectionCount;
updateConnectionLabel();
connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
clients.last()->setReadBufferSize(READ_BUFFER_MAX);
} else {
//If already connected, add to data client list: Add duplicates to data client array
if (connectedClients < MAX_DATA_CLIENTS) {
dataClients.append(client);
++connectedClients;
updateConnectionLabel();
connect(dataClients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
}
}
} else {
//Add to array
clients.append(client);
++connectionCount;
updateConnectionLabel();
connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
clients.last()->setReadBufferSize(READ_BUFFER_MAX);
clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
}
}
To copy to clipboard, switch view to plain text mode
void Server::clientDisconnected()
{
//Remove all the unconnected sockets from the client list
for (int i = 0; i < clients.size(); ++i) {
{
clients.at(i)->close();
clients.at(i)->deleteLater();
clients.removeAt(i);
--connectionCount;
updateConnectionLabel();
}
}
//Remove unconnected data clients from dataclient list
for (int i = 0; i < dataClients.size(); ++i) {
{
dataClients.at(i)->close();
dataClients.at(i)->deleteLater();
dataClients.removeAt(i);
--connectedClients;
updateConnectionLabel();
}
}
QMessageBox::information(this, tr
("DISCONNECT"), tr
("DISCONNECT"));
}
void Server::clientDisconnected()
{
//Remove all the unconnected sockets from the client list
for (int i = 0; i < clients.size(); ++i) {
if (clients.at(i)->state() == QAbstractSocket::UnconnectedState)
{
clients.at(i)->close();
clients.at(i)->deleteLater();
clients.removeAt(i);
--connectionCount;
updateConnectionLabel();
}
}
//Remove unconnected data clients from dataclient list
for (int i = 0; i < dataClients.size(); ++i) {
if (dataClients.at(i)->state() == QAbstractSocket::UnconnectedState)
{
dataClients.at(i)->close();
dataClients.at(i)->deleteLater();
dataClients.removeAt(i);
--connectedClients;
updateConnectionLabel();
}
}
QMessageBox::information(this, tr("DISCONNECT"), tr("DISCONNECT"));
}
To copy to clipboard, switch view to plain text mode
Bookmarks