Hands-On GUI Programming with C++ and Qt5学习笔记 - server-client

在这里插入图片描述

client

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QDebug>
#include <QTcpSocket>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	explicit MainWindow(QWidget *parent = 0);
	~MainWindow();

	void printMessage(QString message);

private slots:
	void on_connectButton_clicked();
	void on_sendButton_clicked();

	void socketConnected();
	void socketDisconnected();
	void socketReadyRead();

private:
	Ui::MainWindow *ui;
	//是否被连接
	bool connectedToHost;
	//连接的套接子
    QTcpSocket* socket;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	connectedToHost = false;
}

MainWindow::~MainWindow()
{
	delete ui;
}

void MainWindow::printMessage(QString message)
{
    //打印消息
	ui->chatDisplay->append(message);
}

void MainWindow::on_connectButton_clicked()
{
	if (!connectedToHost)
	{
        //创建套接子
		socket = new QTcpSocket();
		//连接信号槽
		connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
		connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
		connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
		//连接服务器
		socket->connectToHost("127.0.0.1", 8001);
	}
	else
	{
        //断开连接
		QString name = ui->nameInput->text();
		socket->write("<font color=\"Orange\">" + name.toUtf8() + " has left the chat room.</font>");

		socket->disconnectFromHost();
	}
}

void MainWindow::on_sendButton_clicked()
{
	QString name = ui->nameInput->text();
	QString message = ui->messageInput->text();
	socket->write("<font color=\"Blue\">" + name.toUtf8() + "</font>: " + message.toUtf8());

	ui->messageInput->clear();
}

void MainWindow::socketConnected()
{
	qDebug() << "Connected to server.";

	printMessage("<font color=\"Green\">Connected to server.</font>");

	QString name = ui->nameInput->text();
	socket->write("<font color=\"Purple\">" + name.toUtf8() + " has joined the chat room.</font>");

	ui->connectButton->setText("Disconnect");
	connectedToHost = true;
}

void MainWindow::socketDisconnected()
{
	qDebug() << "Disconnected from server.";

	printMessage("<font color=\"Red\">Disconnected from server.</font>");

	ui->connectButton->setText("Connect");
	connectedToHost = false;
}

void MainWindow::socketReadyRead()
{
	printMessage(socket->readAll());
}

server.h

#ifndef SERVER_H
#define SERVER_H

#include <QObject>

#include <QTcpServer>
#include <QTcpSocket>
#include <QVector>
#include <QDebug>

class server : public QObject
{
	Q_OBJECT
public:
	explicit server(QObject *parent = nullptr);
	void startServer();
	void sendMessageToClients(QString message);

signals:

public slots:
	void newClientConnection();
	void socketDisconnected();
	void socketReadReady();
	void socketStateChanged(QAbstractSocket::SocketState state);

private:
    //服务器指针
	QTcpServer* chatServer;
	//客户端
    QVector<QTcpSocket*>* allClients;
};

#endif // SERVER_H

server.cpp

#include "server.h"

server::server(QObject *parent) : QObject(parent)
{
}
//启动服务器
void server::startServer()
{
    
	allClients = new QVector<QTcpSocket*>;
	
	chatServer = new QTcpServer();
	//设置最大连接数
    chatServer->setMaxPendingConnections(10);
	connect(chatServer, SIGNAL(newConnection()), this, SLOT(newClientConnection()));

	if (chatServer->listen(QHostAddress::Any, 8001))
	{
		qDebug() << "Server has started. Listening to port 8001.";
	}
	else
	{
		qDebug() << "Server failed to start. Error: " + chatServer->errorString();
	}
}

void server::sendMessageToClients(QString message)
{
	if (allClients->size() > 0)
	{
		for (int i = 0; i < allClients->size(); i++)
		{
			if (allClients->at(i)->isOpen() && allClients->at(i)->isWritable())
			{
				allClients->at(i)->write(message.toUtf8());
			}
		}
	}
}

void server::newClientConnection()
{
	QTcpSocket* client = chatServer->nextPendingConnection();
	QString ipAddress = client->peerAddress().toString();
	int port = client->peerPort();

	connect(client, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
	connect(client, SIGNAL(readyRead()),this, SLOT(socketReadReady()));
	connect(client, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));

	allClients->push_back(client);

	qDebug() << "Socket connected from " + ipAddress + ":" + QString::number(port);
}

void server::socketDisconnected()
{
	QTcpSocket* client = qobject_cast<QTcpSocket*>(QObject::sender());
	QString socketIpAddress = client->peerAddress().toString();
	int port = client->peerPort();

	qDebug() << "Socket disconnected from " + socketIpAddress + ":" + QString::number(port);
}

void server::socketReadReady()
{
	QTcpSocket* client = qobject_cast<QTcpSocket*>(QObject::sender());
	QString socketIpAddress = client->peerAddress().toString();
	int port = client->peerPort();

	QString data = QString(client->readAll());

	qDebug() << "Message: " + data + " (" + socketIpAddress + ":" + QString::number(port) + ")";

	sendMessageToClients(data);
}

void server::socketStateChanged(QAbstractSocket::SocketState state)
{
	QTcpSocket* client = qobject_cast<QTcpSocket*>(QObject::sender());
	QString socketIpAddress = client->peerAddress().toString();
	int port = client->peerPort();

	QString desc;

	if (state == QAbstractSocket::UnconnectedState)
		desc = "The socket is not connected.";
	else if (state == QAbstractSocket::HostLookupState)
		desc = "The socket is performing a host name lookup.";
	else if (state == QAbstractSocket::ConnectingState)
		desc = "The socket has started establishing a connection.";
	else if (state == QAbstractSocket::ConnectedState)
		desc = "A connection is established.";
	else if (state == QAbstractSocket::BoundState)
		desc = "The socket is bound to an address and port.";
	else if (state == QAbstractSocket::ClosingState)
		desc = "The socket is about to close (data may still be waiting to be written).";
	else if (state == QAbstractSocket::ListeningState)
		desc = "For internal use only.";

	qDebug() << "Socket state changed (" + socketIpAddress + ":" + QString::number(port) + "): " + desc;
}

main.cpp

#include <QCoreApplication>

#include "server.h"

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	server* myServer = new server();
	myServer->startServer();

	return a.exec();
}

Hands-On High Performance Programming with Qt 5: Build cross-platform applications using concurrency, parallel programming, and memory management Author: Marek Krajewski Pub Date: 2019 ISBN: 978-1789531244 Pages: 384 Language: English Format: EPUB Size: 17 Mb Build efficient and fast Qt applications, target performance problems, and discover solutions to refine your code Achieving efficient code through performance tuning is one of the key challenges faced by many programmers. This book looks at Qt programming from a performance perspective. You’ll explore the performance problems encountered when using the Qt framework and means and ways to resolve them and optimize performance. The book highlights performance improvements and new features released in Qt 5.9, Qt 5.11, and 5.12 (LTE). You’ll master general computer performance best practices and tools, which can help you identify the reasons behind low performance, and the most common performance pitfalls experienced when using the Qt framework. In the following chapters, you’ll explore multithreading and asynchronous programming with C++ and Qt and learn the importance and efficient use of data structures. You’ll also get the opportunity to work through techniques such as memory management and design guidelines, which are essential to improve application performance. Comprehensive sections that cover all these concepts will prepare you for gaining hands-on experience of some of Qt’s most exciting application fields – the mobile and embedded development domains. By the end of this book, you’ll be ready to build Qt applications that are more efficient, concurrent, and performance-oriented in nature What you will learn Understand classic performance best practices Get to grips with modern hardware architecture and its performance impact Implement tools and procedures used in performance optimization Grasp Qt-specific work techniques for graphical user interface (GUI) and platform programming Make Transmission Control Protocol (TCP) and Hypertext Transfer Protocol (HTTP) performant and use the relevant Qt classes Discover the improvements Qt 5.9 (and the upcoming versions) holds in store Explore Qt’s graphic engine architecture, strengths, and weaknesses
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值