I'm writing a program and I have a server that needs to be able to handle multiple client connections at once doing many different things.
I'm using the Qt library, so I handle new connections like this:
In the server constructor:
Server::Server(QObject* parent): QObject(parent)
{
connect(&server, SIGNAL(newConnection()), //&server is a QTcpServer object
this, SLOT(acceptConnection()));
qDebug() << "[" << currentTime() << "] " << "Server started.";
server.listen(QHostAddress::Any, PORT_NUMBER);
qDebug() << "[" << currentTime() << "] " << "Server listening.";
}
The acceptConnection
slot:
void Server::acceptConnection()
{
client = server.nextPendingConnection(); //client is a QTcpSocket* object
connect(client, SIGNAL(readyRead()), //When there is data to be read
this, SLOT(startRead()));
}
The startRead
slot:
void Server::startRead()
{
char serverReceiveBuf[65536]; //A buffer for the data sent to the server
client->read(serverReceiveBuf, client->bytesAvailable());
handleConnection(serverReceiveBuf); //Do something with that data
}
I'm not going to give the handleConnection
function because it's a bit long and I don't think it's necessary for the question.
My worry is latency. If I have one or two users, it's fine. But I haven't tested many concurrent users. There's a synchronization signal sent by the client that asks the server to verify the data the client has matches the data on the server. I'm sending this request every 250 ms, so with a few more concurrent users the client may end up waiting too long every 250 ms because of the other clients sending in synchronization requests.
At least, that's what I believe. Qt is pretty powerful, but I'm not entirely sure if when a signal is received that a new thread is automatically created to handle the connection. If I'm not mistaken, the current way I handle connections involves queueing, so that when the server receives a signal it'll process the requests in the order received. It's important to note that I don't call client->close()
until the client terminates the connection themselves, so once client = server.nextPendingConnecting();
is called client
will always exist until the client terminates it.
Is this a good way of handling multiple connections, given that I'll be regularly having users send synchronization requests often? If not, how should I handle the connections instead?