• qt作业day5


    1. //客户端,
    2. #include "tcpcli.h"
    3. #include "ui_tcpcli.h"
    4. TcpCli::TcpCli(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::TcpCli)
    7. {
    8. ui->setupUi(this);
    9. //给客户端指针实例化对象
    10. cli_sock = new QTcpSocket(this);
    11. ui->discntBtn->setEnabled(false);
    12. // ui->lineEdit->setEnabled(false);
    13. ui->pushButton->setEnabled(false);
    14. connect(cli_sock,&QTcpSocket :: connected,this,&TcpCli :: connected_slot);
    15. connect(cli_sock,&QTcpSocket :: readyRead,this,&TcpCli :: readyRd_slot);
    16. connect(cli_sock,&QTcpSocket :: disconnected,this,&TcpCli :: disconnected_slot);
    17. }
    18. TcpCli::~TcpCli()
    19. {
    20. delete ui;
    21. }
    22. //重写键盘事件
    23. void TcpCli::keyPressEvent(QKeyEvent *event)
    24. {
    25. if(event->text() == "\r")
    26. {
    27. if(connectState != 1)
    28. {
    29. QMessageBox :: critical(this,"错误","未连接服务器");
    30. return;
    31. }
    32. if(ui->lineEdit->text() == "")
    33. {
    34. QMessageBox :: warning(this,"警告","输入不能为空");
    35. return;
    36. }
    37. QString msg = usr_name + ":" + ui->lineEdit->text();
    38. cli_sock->write(msg.toLocal8Bit());
    39. ui->lineEdit->clear();
    40. }
    41. }
    42. //连接服务器
    43. void TcpCli::on_connectBtn_clicked()
    44. {
    45. //获取ui界面的信息
    46. usr_name = ui->usrNameEdit->text();
    47. QString hostName = ui->ipEdit->text();
    48. quint16 port = ui->portEdit->text().toUInt();
    49. cli_sock->connectToHost(hostName,port);
    50. }
    51. void TcpCli::connected_slot()
    52. {
    53. ui->ipEdit->setEnabled(false);
    54. ui->portEdit->setEnabled(false);
    55. ui->pushButton->setEnabled(true);
    56. ui->connectBtn->setEnabled(false);
    57. ui->usrNameEdit->setEnabled(false);
    58. ui->discntBtn->setEnabled(true);
    59. QMessageBox :: information(this,"信息","连接服务器成功");
    60. connectState = 1;
    61. //向服务器发送信息
    62. QString msg = usr_name + ":进入聊天室";
    63. cli_sock->write(msg.toLocal8Bit());
    64. }
    65. //关于readyRead信号对应槽函数的实现
    66. void TcpCli::readyRd_slot()
    67. {
    68. //读取该客户端中的数据
    69. QByteArray msg = cli_sock->readAll();
    70. ui->listWidget->addItem(QString :: fromLocal8Bit(msg));
    71. }
    72. //
    73. void TcpCli::on_pushButton_clicked()
    74. {
    75. QString msg = usr_name + ":"+ ui->lineEdit->text();
    76. cli_sock->write(msg.toLocal8Bit());
    77. //将消息编辑框中的内容清空
    78. ui->lineEdit->clear();
    79. }
    80. //断开连接按钮对应的槽函数
    81. void TcpCli::on_discntBtn_clicked()
    82. {
    83. //准备要发送的信息
    84. QString sndmsg = usr_name + ":离开聊天室";
    85. cli_sock->write(sndmsg.toLocal8Bit());
    86. cli_sock->disconnectFromHost();
    87. ui->ipEdit->setEnabled(true);
    88. ui->portEdit->setEnabled(true);
    89. ui->usrNameEdit->setEnabled(true);
    90. ui->connectBtn->setEnabled(true);
    91. ui->discntBtn->setEnabled(false);
    92. ui->pushButton->setEnabled(false);
    93. connectState = 0;
    94. }
    95. void TcpCli::disconnected_slot()
    96. {
    97. QMessageBox :: information(this,"退出","断开成功");
    98. }
    1. //服务器,源文件
    2. #include "tcpser.h"
    3. #include "ui_tcpser.h"
    4. TcpSer::TcpSer(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::TcpSer)
    7. {
    8. ui->setupUi(this);
    9. //给服务器指针实例化对象
    10. server = new QTcpServer(this);
    11. speech = new QTextToSpeech(this);
    12. }
    13. TcpSer::~TcpSer()
    14. {
    15. delete ui;
    16. }
    17. //启动服务器按钮对应的槽函数
    18. void TcpSer::on_pushButton_clicked()
    19. {
    20. //获取ui界面上的端口号
    21. quint16 port = ui->lineEdit->text().toUInt();
    22. if(!server->listen(QHostAddress :: Any,port))
    23. {
    24. QMessageBox :: critical(this,"错误","服务器启动失败",QMessageBox :: Ok,QMessageBox :: Ok);
    25. return;
    26. }
    27. else
    28. {
    29. QMessageBox :: information(this,"信息","启动成功");
    30. connect(server,&QTcpServer :: newConnection,this,&TcpSer :: newCnt_slot);
    31. }
    32. }
    33. //自定义处理newConnection信号的槽函数实现
    34. void TcpSer::newCnt_slot()
    35. {
    36. speech->setVoice(QVoice());
    37. speech->say("咳咳咳");
    38. QTcpSocket *sock = server->nextPendingConnection();
    39. cli_list.push_back(sock);
    40. connect(sock,&QTcpSocket :: readyRead,this,&TcpSer :: rdyRead_slot);
    41. }
    42. //
    43. void TcpSer::rdyRead_slot()
    44. {
    45. //删除客户端链表中的无效客户端套接字
    46. for(int i = 0;i < cli_list.size();i++)
    47. {
    48. //判断套接字的状态
    49. if(0 == cli_list[i]->state())
    50. {
    51. cli_list.removeAt(i);
    52. }
    53. }
    54. for(int i = 0;i < cli_list.count();i++)
    55. {
    56. if(cli_list[i]->bytesAvailable() != 0)
    57. {
    58. QByteArray msg = cli_list[i]->readAll();
    59. //将数据展示到ui界面上
    60. ui->listWidget->addItem(QString :: fromLocal8Bit(msg));
    61. //将接收到的信息发送给所有客户端
    62. for(int j = 0;j < cli_list.count();j++)
    63. {
    64. cli_list[j]->write(msg);
    65. }
    66. }
    67. }
    68. }

    运行效果:服务器端,开启后

  • 相关阅读:
    计算机考研考研院校难度等级,建议收藏
    Asp-Net-Core开发笔记:给SwaggerUI加上登录保护功能
    七夕专属博文-使用QGraphics画“红心“或“黑心“(含数学模型讲解)
    [附源码]计算机毕业设计JAVA儒家文化网站
    景联文科技入选量子位智库《中国AIGC数据标注产业全景报告》数据标注行业代表机构
    暑期JAVA学习(34)线程同步
    python将visio转换为 PDF 文件
    Flink / Scala - 使用 RedisSink 存储数据
    leetcode(力扣) 53. 最大子数组和 (动态规划)
    Java程序设计——反射(Java高级应用)
  • 原文地址:https://blog.csdn.net/a136630108/article/details/132684000