Qt NetWork即时通讯网络聊天室(基于TCP)
本文使用QT的网络模块来创建一个网络聊天室程序,主要包括以下功能:
1、基于TCP的可靠连接(QTcpServer、QTcpSocket)
2、一个服务器,多个客户端
3、服务器接收到某个客户端的请求以及发送信息,经该信息重定向发给其它客户端
最终实现一个共享聊天内容的聊天室!
开发测试环境:QT5.12.0 + Qt Creator 4.8.0 + MinGW7.3
代码如下:
1、服务器 QtInstantMessagingServer
基于Console的应用程序,因为这里不需要界面。
QT += core network
QT -= gui
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#ifndef SERVER_H
#define SERVER_H #include <QObject> class Server : public QObject void startServer(); signals: public slots: private: #endif // SERVER_H |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
#include "Server.h"
Server::Server(QObject *parent) : QObject(parent) } void Server::startServer() } void Server::sendMessageToClients(QString message) } void Server::newClientConnection() // When a client is disconnected from the server, the disconnected() signal will be triggered // whenever a client is sending in a message to the server, the readyRead() signal will be triggered. // This function gets triggered whenever a client's network state has changed, |
1
2 3 4 5 6 7 8 9 10 11 12 13 |
#include <QCoreApplication>
#include "Server.h" int main(int argc, char *argv[]) Server* myServer = new Server(); return a.exec(); |
2、客户端QtInstantMessagingClient
基于Widget的应用程序,客户端需要一个友好的界面,父类QMainWindow,MainWindow.ui定义界面如下:
可以给不同的客户端取个名字,如“Michael”、“James”等等,点击“Connect”按钮连接服务端,此时Label变为“Disconnect”。
QT += core gui network
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow : public QMainWindow public: private slots: void on_sendButton_clicked(); private: void printMessage(QString message); #endif // MAINWINDOW_H |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include "MainWindow.h"
#include "ui_MainWindow.h" MainWindow::MainWindow(QWidget *parent) : MainWindow::~MainWindow() void MainWindow::on_connectButton_clicked() } void MainWindow::socketConnected() void MainWindow::socketDisconnected() void MainWindow::socketReadyRead() void MainWindow::printMessage(QString message) void MainWindow::on_sendButton_clicked() |
1
2 3 4 5 6 7 8 9 10 11 12 13 |
#include "MainWindow.h"
#include <QApplication> int main(int argc, char *argv[]) return a.exec(); |
构建成功后,将生成的QtInstantMessagingServer.exe以及QtInstantMessagingClient.exe置于.\Qt\Qt5.12.0\5.12.0\mingw73_64\bin目录下(该目录下可以双击exe直接运行!)
最新文章
- HTML5横竖屏提示
- HTML随学随机
- [ASE][Daily Scrum]11.13
- Mac nginx PCRE install ngnix
- 如何debug ruby
- IBM Java架构师的技能
- 1639: [Usaco2007 Mar]Monthly Expense 月度开支
- JavaSE项目之聊天室
- MLlib--决策树
- 我的第一个.NET Core App Windows系统
- Dynamics 365 Online-Microsoft Flow
- SQL Server 本地数据库登录不上 解决方法
- 【BZOJ3816】【清华集训2014】矩阵变换 稳定婚姻问题
- 从RGB扫描图到数字化等高线矢量图
- What Your Computer Does While You Wait.CPU的等待有多久?
- UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]
- Python学习(002)--Python介绍
- [转]Outlook HTML渲染
- java 项目中类找不到异常解决办法
- shell相关文件
热门文章
- Docker Compose YML文件配置
- A Comparison of Serverless Frameworks for Kubernetes: OpenFaas, OpenWhisk, Fission, Kubeless and more
- 初识PIXI.js
- LeetCode 1094. Car Pooling
- python3 pathlib库中的Path类的使用
- 不用FTP,直接Windows与Linux下互传文件
- 秘制牛肉Gamma阶段项目展示
- windows server 2008的系统备份
- python 判断矩阵中每行非零个数的方法
- | C语言I作业04