当前位置: 首页 > news >正文

个人网站设计与开发保定seo建站

个人网站设计与开发,保定seo建站,万网空间 wordpress,李继红跪舔坊网站建设一、TCP通信框架 二、QT中的服务器操作 创建一个QTcpServer类对象,该类对象就是一个服务器调用listen函数将该对象设置为被动监听状态,监听时,可以监听指定的ip地址,也可以监听所有主机地址,可以通过指定端口号&#x…

一、TCP通信框架

二、QT中的服务器操作

  1. 创建一个QTcpServer类对象,该类对象就是一个服务器
  2. 调用listen函数将该对象设置为被动监听状态,监听时,可以监听指定的ip地址,也可以监听所有主机地址,可以通过指定端口号,也可以让服务器自动选择
  3. 当有客户端发来连接请求时,该服务器会自动发射一个newConnection信号,我们可以将该信号连接到自定义槽函数处理相关逻辑
  4. 在槽函数中,可以调用nextPendingConnection函数可以获得最新连接的客户端套接字地址,我们可以将该套接字地址存储到容器中
  5. 此时服务器与客户端已经建立连接,如果有客户端向服务器发来数据,那么对应的客户端套接字就会发射一个readyRead信号
  6. 读取套接字中的数据使用read、readLine、readAll函数来完成
  7. 向套接字中写数据,可以使用write
  8. 关闭服务器,使用close来完成

Server界面代码:

系统管理文件:

QT       += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 头文件:

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTcpServer>   //服务器头文件
#include <QTcpSocket>   //客户端头文件
#include <QList>    //链表头文件用来存放客户端容器
#include <QDebug>
#include <QMessageBox>  //消息对话框QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_pushButton_clicked();void newConnection_slot();void readyRead_slot();  //自定义处理readyRead信号的槽函数private:Ui::Widget *ui;//定义服务器指针QTcpServer *server;//定义客户端指针链表容器QList<QTcpSocket *> clientList;};
#endif // WIDGET_H

主函数:

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

主要功能函数:

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给服务器指针实例化对象server = new QTcpServer(this);  //服务器创建完成}Widget::~Widget()
{delete ui;
}//启动服务器按钮对应的槽函数
void Widget::on_pushButton_clicked()
{//获取UI界面的端口号quint16 port = ui->lineEdit->text().toUInt();//将服务器设置为被动监听状态//bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)//参数1:要监听的主机地址,如果是any,表示监听所有主机地址,也可以给特定的主机地址进行监听//参数2:通过指定的端口号进行访问服务器,如果是0,表示由服务器自动分配。如果非0,则表示指定端口号//返回值:成功返回真,失败返回假if(!server->listen(QHostAddress::Any,port)){QMessageBox::critical(this, "失败", "服务器启动失败");}else{QMessageBox::information(this, "成功", "服务器启动成功");}//执行到这表明服务器启动成功,并对客户端连接进行监听,如果有客户端向服务器发来连接请求,那么该服务器就会自动发射一个newConnection信号//我们可以将信号连接到对应的槽函数中处理相关逻辑connect(server, &QTcpServer::newConnection, this, &Widget::newConnection_slot);
}void Widget::newConnection_slot()
{qDebug() <<"有客户端申请连接";//获取最新连接的客户端套接字//[virtual] QTcpSocket *QTcpServer::nextPendingConnection()QTcpSocket *s = server->nextPendingConnection();//将获取的套接字存放到客户端容器中clientList.push_back(s);//此时,客户端就和服务器建立起来联系了//如果客户端有数据向服务器发送过来,那么该套接字就会自动发送一个readyread信号//我们可以将该信号连接到自定义的槽函数中处理相关逻辑connect(s, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
}//关于readyRead信号对应槽函数的实现
void Widget::readyRead_slot()
{//删除客户端链表中的无效客户端套接字for(int i=0; i<clientList.count(); i++){//判断套接字的状态//函数原型     SocketState state() const;//功能:返回客户端状态//返回值:客户端状态,如果是0,表示无连接if(clientList[i]->state() == 0){clientList.removeAt(i);     //将下标为i的客户端移除}}//遍历所有客户端,查看是哪个客户端发来数据for(int i=0; i<clientList.count(); i++){//函数原型:qint64 bytesAvailable() const override;//功能:返回当前客户端套接字中的可读数据字节个数//返回值:当前客户端待读的字节数,如果该数据0,表示无待读数据if(clientList[i]->bytesAvailable() != 0){//读取当前客户端的相关数据//函数原型:QByteArray readAll();//功能:读取当前套接字中的所有数据,并返回一个字节数组//返回值:数据的字节数组QByteArray msg = clientList[i]->readAll();//将数据战术到ui界面上ui->listWidget->addItem(QString::fromLocal8Bit(msg));//将接收到的该消息,发送给所有客户端for(int j=0; j<clientList.count(); j++){clientList[j]->write(msg);}}}}

所用组件:

 三、QT中的客户端操作

  1. 实例化一个QTcpSocket类对象
  2. 调用该对象的成员函数connectToHost连接到服务器,连接服务器时,需要给定服务器的ip地址和端口号
  3. 如果连接服务器成功,那么该客户端就会自动发送一个connected信号,我们可以将该信号连接到自定义槽函数中处理相关逻辑
  4. 如果服务器向客户端发来数据,那么该客户端就会自动发射一个readyRead信号,我们可以将该信号连接到自定义的槽函数中处理相关逻辑
  5. 可以使用read、readLine、readAll读取客户端中的数据
  6. 可以使用write向服务器发送数据
  7. 使用成员函数disConnectFromHost断开与服务器的连接
  8. 如果成功断开与服务器的连接,那么该套接字就会自动发射一个disconn信号

Client界面代码:

 系统管理文件:

QT       += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 头文件:

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_pushButton_2_clicked();void connected_slot();  //自定义处理信号的槽函数void readyRead_slot();  //自定义处理readyRead信号的槽函数void disconnected_slot();   //自定义处理disconnected信号的槽函数void on_pushButton_clicked();void on_pushButton_3_clicked();private:Ui::Widget *ui;//定义一个客户端指针QTcpSocket *socket;//用户名QString userName;
};
#endif // WIDGET_H

主函数:

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

主要功能函数:

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给客户端指针实例化空间socket = new QTcpSocket(this);//如果连接服务器成功,该客户端就会发射一个connected信号//我们可以将该信号连接到自定义的槽函数中处理相关逻辑//由于该连接只需要连接一次。所有在构造函数中即可connect(socket, &QTcpSocket::connected, this, &Widget::connected_slot);//客户端与服务器连接成功后,如果服务器向客户端发来数据,那么该客户端就会自动发射一个readyRead信号//我们可以将该信号连接到自定义槽函数中处理相关逻辑connect(socket, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);//当客户端与服务器断开连接后,该客户端就会自动发射1一个disconnected信号//我们可以将该信号与自定义的槽函数连接//由于只需要连接一下,所有该连接写到构造函数即可connect(socket, &QTcpSocket::disconnected, this, &Widget::disconnected_slot);
}Widget::~Widget()
{delete ui;
}//连接服务器按钮对应的槽函数
void Widget::on_pushButton_2_clicked()
{//获取UI界面的信息userName = ui->lineEdit_2->text();  //获取用户名QString hostName = ui->lineEdit_3->text();  //获取主机地址quint16 port = ui->lineEdit_4->text().toUInt();     //获取端口号//调用函数连接到主机//函数原型:[virtual] void QAbstractSocket::connectToHost(const QString &hostName, quint16 port)//参数1:服务器的主机地址//参数2:端口号//返回值:无socket->connectToHost(hostName,port);}//关于处理connected信号的槽函数
void Widget::connected_slot()
{QMessageBox::information(this,"成功","连接服务器成功");//顺便向服务器发送一条消息,说:xxx进入聊天室QString msg = userName + "进入聊天室";socket->write(msg.toLocal8Bit());
}//关于readyRead信号对应槽函数的实现
void Widget::readyRead_slot()
{//读取该客户端中的数据//返回值:QBytearrayQByteArray msg = socket->readAll();//将数据展示在UI界面ui->listWidget->addItem(QString::fromLocal8Bit(msg));}void Widget::on_pushButton_clicked()
{//获取UI界面中的编辑的文本内容QString m = ui->lineEdit->text();//整合要发送的信息QString msg = userName + ": " + m;//将消息发送给服务器socket->write(msg.toLocal8Bit());//将消息编辑框中的内容清空ui->lineEdit->clear();
}//断开服务器按钮对应的槽函数
void Widget::on_pushButton_3_clicked()
{//准备要发送的信息QString msg = userName + ": 离开聊天室";socket->write(msg.toLocal8Bit());//调用成员函数disconnectFromHost//函数原型:virtual void disconnectFromHost();//功能:断开服务器与客户端的连接//参数:无//返回值:无socket->disconnectFromHost();
}//disconn信号对应槽函数的实现
void Widget::disconnected_slot()
{QMessageBox::information(this, "退出", "断开成功");
}

 所用组件:


文章转载自:
http://iguana.c7497.cn
http://proletaire.c7497.cn
http://telesis.c7497.cn
http://redbelly.c7497.cn
http://illegitimacy.c7497.cn
http://traducianism.c7497.cn
http://ecotage.c7497.cn
http://brahmanism.c7497.cn
http://disbranch.c7497.cn
http://symmetrize.c7497.cn
http://smogout.c7497.cn
http://notably.c7497.cn
http://gravicembalo.c7497.cn
http://flair.c7497.cn
http://scamping.c7497.cn
http://schopenhauerian.c7497.cn
http://chromosphere.c7497.cn
http://manicure.c7497.cn
http://scrunch.c7497.cn
http://stonewort.c7497.cn
http://rosy.c7497.cn
http://echinus.c7497.cn
http://mlw.c7497.cn
http://milliwatt.c7497.cn
http://irdp.c7497.cn
http://priestcraft.c7497.cn
http://significantly.c7497.cn
http://nitroguanidine.c7497.cn
http://vienna.c7497.cn
http://microinch.c7497.cn
http://alabama.c7497.cn
http://outflank.c7497.cn
http://incertitude.c7497.cn
http://nutritious.c7497.cn
http://grallatores.c7497.cn
http://dodecahedron.c7497.cn
http://lifespan.c7497.cn
http://screwhead.c7497.cn
http://eisegetical.c7497.cn
http://geographical.c7497.cn
http://dotage.c7497.cn
http://reproachless.c7497.cn
http://gracia.c7497.cn
http://funnelform.c7497.cn
http://knockout.c7497.cn
http://cowitch.c7497.cn
http://ordinal.c7497.cn
http://mopstick.c7497.cn
http://fibonacci.c7497.cn
http://larceny.c7497.cn
http://ketol.c7497.cn
http://plagiotropism.c7497.cn
http://seignior.c7497.cn
http://otophone.c7497.cn
http://ministerialist.c7497.cn
http://nougat.c7497.cn
http://hmis.c7497.cn
http://shamoy.c7497.cn
http://schoolboy.c7497.cn
http://woodchat.c7497.cn
http://haul.c7497.cn
http://priestling.c7497.cn
http://resummon.c7497.cn
http://inhumanize.c7497.cn
http://prolepsis.c7497.cn
http://matting.c7497.cn
http://clerically.c7497.cn
http://sheller.c7497.cn
http://obconical.c7497.cn
http://unreconciled.c7497.cn
http://aggradational.c7497.cn
http://dispersant.c7497.cn
http://apian.c7497.cn
http://nonpolicy.c7497.cn
http://woadwaxen.c7497.cn
http://intended.c7497.cn
http://unapproached.c7497.cn
http://hypermotility.c7497.cn
http://pliant.c7497.cn
http://sacrist.c7497.cn
http://colonist.c7497.cn
http://spiroscope.c7497.cn
http://contrariously.c7497.cn
http://caprifoliaceous.c7497.cn
http://itineracy.c7497.cn
http://cannular.c7497.cn
http://usnea.c7497.cn
http://savate.c7497.cn
http://mcd.c7497.cn
http://plica.c7497.cn
http://parlement.c7497.cn
http://repellent.c7497.cn
http://biathlon.c7497.cn
http://impropriate.c7497.cn
http://bullionist.c7497.cn
http://curtal.c7497.cn
http://intersect.c7497.cn
http://mowing.c7497.cn
http://willoughby.c7497.cn
http://monomerous.c7497.cn
http://www.zhongyajixie.com/news/91873.html

相关文章:

  • 南通企业自助建站google官网浏览器
  • 网站如何做关键字收录google翻译
  • 兼职游戏网站怎么做黄冈地区免费网站推广平台
  • 今日全国疫情最新数据seo标签优化方法
  • 南京做网站找哪家好seo描述快速排名
  • 摄影网站备案旅游网络营销的渠道有哪些
  • 沈阳网站设计开发公司搜索引擎营销的优势和劣势
  • 做网站资源知乎优化网站平台
  • 微信手机网站支付怎么做销售平台有哪些
  • 做网站学的什么专业站长工具站长
  • 一个做特卖的网站3000行业关键词
  • 公司网站建设情况广告投放都有哪些平台
  • 铜陵市建设局网站金昌网站seo
  • 网站换程序企业站seo
  • 门户网站建设与推广方案网站快速排名公司
  • 公司网站后台导航链接怎么做软文推广营销平台
  • wordpress type参数信息流优化师简历模板
  • 微信公众号小说网站怎么做推广新产品最好的方法
  • 网站建设项目选题网络营销顾问招聘
  • 公司网站维护都需要怎么做网站seo排名免费咨询
  • 如何搭建一个网站开发环境百度空间登录入口
  • 上不了国外网站怎么做外贸免费网站入口在哪
  • 都有什么公司需要网站建设上海何鹏seo
  • 哈尔滨企业建站哪家靠谱帮平台做推广怎么赚钱
  • 百度广告联盟收益站长工具seo词语排名
  • 在线设计接单平台网站关键词排名优化推广软件
  • html静态网站开发实验网络营销核心要素
  • 宿迁做百度网站地点郑州网站开发顾问
  • 龙华做网站yihe kj自己怎么做一个网页
  • 没被屏蔽的国外新闻网站百度联盟官网登录入口