SimpleChatClient
::SimpleChatClient(QWidget* parent, Qt
::WFlags flags
) : QWidget(parent, flags
) {
port->setRange(1, 32767);
port->setValue(DEFAULT_PORT);
server->setText("localhost");
top->addWidget(label, 0, 0);
top->addWidget(server, 0, 1);
top->addWidget(port, 0, 2);
label
= new QLabel("Nick:",
this);
nick->setText("Anonymous");
top->addWidget(label, 1, 0);
top->addWidget(nick, 1, 1);
top->addWidget(conn, 1, 2);
chat->setReadOnly(true);
label
= new QLabel("Message:",
this);
send->setDefault(true);
bottom->addWidget(label);
bottom->addWidget(message);
bottom->addWidget(send);
main->addLayout(top);
main->addWidget(chat);
main->addLayout(bottom);
setLayout(main);
socket = new QSslSocket(this);
connect(message, SIGNAL(returnPressed()), SLOT(sendMessage()));
connect(send, SIGNAL(clicked()), SLOT(sendMessage()));
connect(conn, SIGNAL(clicked()), SLOT(toggleConnection()));
connect(socket, SIGNAL(connected()), SLOT(setConnected()));
connect(socket, SIGNAL(disconnected()), SLOT(setDisconnected()));
connect(socket, SIGNAL(readyRead()), SLOT(receiveMessage()));
connect(socket, SIGNAL(encrypted()), this, SLOT(connectionReady()));
setDisconnected();
}
void SimpleChatClient::connectionReady() {
qDebug() << "Connection is encrypted! ";
}
SimpleChatClient::~SimpleChatClient() {
buffer->close();
}
void SimpleChatClient::setConnected() {
qDebug() << "Connected! ";
port->setEnabled(false);
server->setEnabled(false);
nick->setEnabled(true);
message->setEnabled(true);
chat->setEnabled(true);
chat->clear();
send->setEnabled(true);
conn->setText("Disconnect");
}
void SimpleChatClient::setDisconnected() {
qDebug() << "Disconnected! ";
port->setEnabled(true);
server->setEnabled(true);
nick->setEnabled(false);
message->setEnabled(false);
chat->setEnabled(false);
send->setEnabled(false);
conn->setText("Connect");
}
void SimpleChatClient::toggleConnection() {
qDebug() << "Connecting! ";
socket->connectToHostEncrypted(server->text(), port->value());
} else {
qDebug() << "Disconnecting! ";
socket->disconnectFromHost();
}
}
void SimpleChatClient::sendMessage() {
// "<nick> message\n"
socket->write("<" + nick->text().toLatin1() + "> " + message->text().toLatin1() + "\n");
message->clear();
}
void SimpleChatClient::receiveMessage() {
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine()) {
chat->append(line.simplified());
}
}
SimpleChatClient::SimpleChatClient(QWidget* parent, Qt::WFlags flags) : QWidget(parent, flags) {
QVBoxLayout* main = new QVBoxLayout(this);
QGridLayout* top = new QGridLayout;
QHBoxLayout* bottom = new QHBoxLayout;
QLabel* label = new QLabel("Server:", this);
server = new QLineEdit(this);
port = new QSpinBox(this);
conn = new QPushButton("Connect", this);
port->setRange(1, 32767);
port->setValue(DEFAULT_PORT);
server->setText("localhost");
top->addWidget(label, 0, 0);
top->addWidget(server, 0, 1);
top->addWidget(port, 0, 2);
label = new QLabel("Nick:", this);
nick = new QLineEdit(this);
nick->setText("Anonymous");
top->addWidget(label, 1, 0);
top->addWidget(nick, 1, 1);
top->addWidget(conn, 1, 2);
chat = new QTextEdit(this);
chat->setReadOnly(true);
label = new QLabel("Message:", this);
message = new QLineEdit(this);
send = new QPushButton("Send", this);
send->setDefault(true);
bottom->addWidget(label);
bottom->addWidget(message);
bottom->addWidget(send);
main->addLayout(top);
main->addWidget(chat);
main->addLayout(bottom);
setLayout(main);
buffer = new QBuffer(this);
socket = new QSslSocket(this);
buffer->open(QIODevice::ReadWrite);
connect(message, SIGNAL(returnPressed()), SLOT(sendMessage()));
connect(send, SIGNAL(clicked()), SLOT(sendMessage()));
connect(conn, SIGNAL(clicked()), SLOT(toggleConnection()));
connect(socket, SIGNAL(connected()), SLOT(setConnected()));
connect(socket, SIGNAL(disconnected()), SLOT(setDisconnected()));
connect(socket, SIGNAL(readyRead()), SLOT(receiveMessage()));
connect(socket, SIGNAL(encrypted()), this, SLOT(connectionReady()));
setDisconnected();
}
void SimpleChatClient::connectionReady() {
qDebug() << "Connection is encrypted! ";
}
SimpleChatClient::~SimpleChatClient() {
buffer->close();
}
void SimpleChatClient::setConnected() {
qDebug() << "Connected! ";
port->setEnabled(false);
server->setEnabled(false);
nick->setEnabled(true);
message->setEnabled(true);
chat->setEnabled(true);
chat->clear();
send->setEnabled(true);
conn->setText("Disconnect");
}
void SimpleChatClient::setDisconnected() {
qDebug() << "Disconnected! ";
port->setEnabled(true);
server->setEnabled(true);
nick->setEnabled(false);
message->setEnabled(false);
chat->setEnabled(false);
send->setEnabled(false);
conn->setText("Connect");
}
void SimpleChatClient::toggleConnection() {
if (socket->state() == QAbstractSocket::UnconnectedState) {
qDebug() << "Connecting! ";
socket->connectToHostEncrypted(server->text(), port->value());
} else {
qDebug() << "Disconnecting! ";
socket->disconnectFromHost();
}
}
void SimpleChatClient::sendMessage() {
// "<nick> message\n"
socket->write("<" + nick->text().toLatin1() + "> " + message->text().toLatin1() + "\n");
message->clear();
}
void SimpleChatClient::receiveMessage() {
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine()) {
QString line = buffer->readLine();
chat->append(line.simplified());
}
}
To copy to clipboard, switch view to plain text mode
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}
SimpleChatServer::~SimpleChatServer() {
}
////////////////////////////////////////////////////////////////////////////////////////////////
QSslSocket *SimpleChatServer::nextPendingConnection() {
if (pendingConnections.isEmpty())
return 0;
return pendingConnections.takeFirst();
}
void SimpleChatServer::incomingConnection(int socketDescriptor) {
qDebug() << "New Connection! ";
QSslSocket *serverSocket = new QSslSocket;
if (serverSocket->setSocketDescriptor(socketDescriptor)) {
qDebug() << "Socket Descriptor Set! ";
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(connectionReady()));
serverSocket->startServerEncryption();
pendingConnections.append(serverSocket);
} else {
qDebug() << "Socket Descriptor Not Set! ";
delete serverSocket;
}
}
void SimpleChatServer::connectionReady() {
qDebug() << "Connection is encrypted! ";
}
////////////////////////////////////////////////////////////////////////////////////////////////
void SimpleChatServer::addConnection() {
qDebug() << "Connection added to list! ";
QSslSocket* connection = nextPendingConnection();
connections.append(connection);
buffers.insert(connection, buffer);
connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
}
void SimpleChatServer::removeConnection() {
qDebug() << "Disconnected! ";
QSslSocket* socket = static_cast<QSslSocket*>(sender());
QBuffer* buffer
= buffers.
take(socket
);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void SimpleChatServer::receiveMessage() {
QSslSocket* socket = static_cast<QSslSocket*>(sender());
QBuffer* buffer
= buffers.
value(socket
);
// missing some checks for returns values for the sake of simplicity
qDebug() << "Reading From: " << socket;
qint64 bytes = buffer->write(socket->readAll());
//now we have a buffer containing the data incomming from the socket
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine()) {
//this list holds the sockets currently connected:
foreach (QSslSocket* connection, connections) {
qDebug() << "Writing to: " << connection;
connection->write(line);
}
}
}
SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent) {
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}
SimpleChatServer::~SimpleChatServer() {
}
////////////////////////////////////////////////////////////////////////////////////////////////
QSslSocket *SimpleChatServer::nextPendingConnection() {
if (pendingConnections.isEmpty())
return 0;
return pendingConnections.takeFirst();
}
void SimpleChatServer::incomingConnection(int socketDescriptor) {
qDebug() << "New Connection! ";
QSslSocket *serverSocket = new QSslSocket;
if (serverSocket->setSocketDescriptor(socketDescriptor)) {
qDebug() << "Socket Descriptor Set! ";
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(connectionReady()));
serverSocket->startServerEncryption();
pendingConnections.append(serverSocket);
} else {
qDebug() << "Socket Descriptor Not Set! ";
delete serverSocket;
}
}
void SimpleChatServer::connectionReady() {
qDebug() << "Connection is encrypted! ";
}
////////////////////////////////////////////////////////////////////////////////////////////////
void SimpleChatServer::addConnection() {
qDebug() << "Connection added to list! ";
QSslSocket* connection = nextPendingConnection();
connections.append(connection);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(connection, buffer);
connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
}
void SimpleChatServer::removeConnection() {
qDebug() << "Disconnected! ";
QSslSocket* socket = static_cast<QSslSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void SimpleChatServer::receiveMessage() {
QSslSocket* socket = static_cast<QSslSocket*>(sender());
QBuffer* buffer = buffers.value(socket);
// missing some checks for returns values for the sake of simplicity
qDebug() << "Reading From: " << socket;
qint64 bytes = buffer->write(socket->readAll());
//now we have a buffer containing the data incomming from the socket
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine()) {
QByteArray line = buffer->readLine();
//this list holds the sockets currently connected:
foreach (QSslSocket* connection, connections) {
qDebug() << "Writing to: " << connection;
connection->write(line);
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks