Hi everybody,
I'm new to Qt and this forum. After my first steps in the last few weeks with Qt, I tried to write a server which can handle multiple clients. The basis for my program is "Multi client server without threading" of this site.
My methode Server::start() ist connected with a button, also the methode Server::stop(). When I start my server all works fine, but when I try to stop my server and restart it again with the buttons, my Server crashes as soon as I want to connect with the client. Has anybody of you an idea for a solution for my problem?
Here my code:
// network.h
#ifndef NETWORK_H
#define NETWORK_H
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QList>
#include <QHash>
#include <QBuffer>
#include "mainwindow.h"
Q_OBJECT
protected:
protected slots:
void handleNewConnection();
void clientDisconnected();
void start(int port = 10002);
void stop();
void read();
private:
QList<QTcpSocket *> clientConnections;
QHash<QTcpSocket
*,
QBuffer*> buffers;
public:
explicit Server();
~Server();
};
#endif // NETWORK_H
// network.h
#ifndef NETWORK_H
#define NETWORK_H
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QList>
#include <QHash>
#include <QBuffer>
#include "mainwindow.h"
class Server : public QTcpServer {
Q_OBJECT
protected:
void sendHello(QTcpSocket *client);
protected slots:
void handleNewConnection();
void clientDisconnected();
void start(int port = 10002);
void stop();
void read();
private:
QList<QTcpSocket *> clientConnections;
QHash<QTcpSocket*, QBuffer*> buffers;
public:
explicit Server();
~Server();
};
#endif // NETWORK_H
To copy to clipboard, switch view to plain text mode
// network.c
#include "network.h"
Server::Server() {
}
Server::~Server() {
stop();
}
void Server::stop() {
close();
qDeleteAll(clientConnections);
}
void Server::start(int port) {
return;
}
connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
}
void Server::handleNewConnection() {
while (hasPendingConnections()) {
buffers.insert(client, buffer);
connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(client, SIGNAL(readyRead()), this, SLOT(read()));
clientConnections.append(client);
sendHello(client);
}
}
void Server::clientDisconnected() {
QTcpSocket *client
= qobject_cast<QTcpSocket
*>
(sender
());
if(!client) return;
clientConnections.removeAll(client);
client->deleteLater();
}
if(!client) return;
client->write("Hello\n");
}
void Server::read() {
QTcpSocket *client
= qobject_cast<QTcpSocket
*>
(sender
());
QBuffer* buffer
= buffers.
value(client
);
if(!client) return;
qint64 bytes = buffer->write(client->readAll());
buffer->seek(buffer->pos() - bytes);
while (buffer->canReadLine()) {
switch(line[0]) {
case 'a' : client->write(line);
break;
}
}
}
// network.c
#include "network.h"
Server::Server() {
}
Server::~Server() {
stop();
}
void Server::stop() {
close();
qDeleteAll(clientConnections);
}
void Server::start(int port) {
if(!listen(QHostAddress::Any, port)) {
return;
}
connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
}
void Server::handleNewConnection() {
while (hasPendingConnections()) {
QTcpSocket *client = nextPendingConnection();
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(client, buffer);
connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(client, SIGNAL(readyRead()), this, SLOT(read()));
clientConnections.append(client);
sendHello(client);
}
}
void Server::clientDisconnected() {
QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
if(!client) return;
clientConnections.removeAll(client);
client->deleteLater();
}
void Server::sendHello(QTcpSocket *client) {
if(!client) return;
client->write("Hello\n");
}
void Server::read() {
QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
QBuffer* buffer = buffers.value(client);
if(!client) return;
qint64 bytes = buffer->write(client->readAll());
buffer->seek(buffer->pos() - bytes);
while (buffer->canReadLine()) {
QByteArray line = buffer->readLine();
switch(line[0]) {
case 'a' : client->write(line);
break;
}
}
}
To copy to clipboard, switch view to plain text mode
Thank you in advance.
Bookmarks