zAAm
19th April 2010, 10:24
Hi
I'm trying to build a streaming server/client program that accommodates multiple clients. I have to keep track of all of them at any one time to determine which one sent a message as ALL data has to be multiplexed through port 4012.
At the moment I'm allocating a new QTcpSocket object each time a new connection is made and keeping track of them through a QList object, but I keep getting heap corruption errors on destruction. I think it may be because of the wrong allocation of sockets or through the destruction of them.
I've been struggling with this for a while now and it's very frustrating as the errors aren't consistent.
Here's the code when QTcpServer signals newConnection(): (ClientSocket inherits from QTcpSocket)
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);
}
}
The destruction code at this time (I've tried various methods):
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"));
}
Thanks for any suggestions :)
I'm trying to build a streaming server/client program that accommodates multiple clients. I have to keep track of all of them at any one time to determine which one sent a message as ALL data has to be multiplexed through port 4012.
At the moment I'm allocating a new QTcpSocket object each time a new connection is made and keeping track of them through a QList object, but I keep getting heap corruption errors on destruction. I think it may be because of the wrong allocation of sockets or through the destruction of them.
I've been struggling with this for a while now and it's very frustrating as the errors aren't consistent.
Here's the code when QTcpServer signals newConnection(): (ClientSocket inherits from QTcpSocket)
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);
}
}
The destruction code at this time (I've tried various methods):
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"));
}
Thanks for any suggestions :)