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

惠州技术支持网站建设百度怎样注册免费的网站

惠州技术支持网站建设,百度怎样注册免费的网站,旅游产品推广方案,个人网站备案号被注销Qt 实现TCP通信和UDP通信 1、TCP通信 QT中实现TCP通信主要用到了以下类#xff1a;QTcpServer、QTcpSocket、QHostAddress等#xff1b; 使用QTcpServer来创建一个TCP服务器#xff0c;在新的连接建立时#xff0c;将新建立连接的socket添加到列表中#xff0c;以便发送…Qt 实现TCP通信和UDP通信 1、TCP通信 QT中实现TCP通信主要用到了以下类QTcpServer、QTcpSocket、QHostAddress等 使用QTcpServer来创建一个TCP服务器在新的连接建立时将新建立连接的socket添加到列表中以便发送数据同时监听在指定的IP地址和端口上并在有新的客户端连接上来时进行处理使用QTcpSocket来创建一个TCP客户端连接到服务器并发送数据 示例代码 1创建两个头文件(TcpServer.h 和 TcpClient.h) TcpServer.h #ifndef TCPSERVER_H #define TCPSERVER_H#include QObject #include QTcpServer #include QTcpSocketclass TcpServer : public QObject {Q_OBJECT public:explicit TcpServer(QObject *parent nullptr);signals:public slots:void startServer();void newClientConnection();void readData();void clientDisconnected();private:QTcpServer *tcpServer;QListQTcpSocket * clients; };#endif // TCPSERVER_H TcpClient.h #ifndef TCPCLIENT_H #define TCPCLIENT_H#include QObject #include QTcpSocketclass TcpClient : public QObject {Q_OBJECT public:explicit TcpClient(QObject *parent nullptr);public slots:void onConnected();void onDisconnected();void onError(QAbstractSocket::SocketError error);void onReadyRead();void connectToServer(QString ipAddress, int port);void sendData(QByteArray data);private:QTcpSocket *socket; };#endif // TCPCLIENT_H 2创建两个源文件(TcpServer.cpp 和 TcpClient.cpp) TcpServer.cpp #include tcpserver.h #include QDebugTcpServer::TcpServer(QObject *parent) : QObject(parent) { }void TcpServer::startServer() {tcpServer new QTcpServer(this);connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClientConnection()));if (!tcpServer-listen(QHostAddress::Any, 1234)){qDebug() Unable to start the server: tcpServer-errorString();return;}qDebug() Listening on QHostAddress::Any : 1234; }void TcpServer::newClientConnection() {while (tcpServer-hasPendingConnections()){QTcpSocket *client tcpServer-nextPendingConnection();clients.push_back(client);QObject::connect(client, SIGNAL(readyRead()), this, SLOT(readData()));QObject::connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));} }void TcpServer::readData() {QTcpSocket *sender static_castQTcpSocket*(QObject::sender());QByteArray data sender-readAll();qDebug() Received: data; }void TcpServer::clientDisconnected() {QTcpSocket *sender static_castQTcpSocket*(QObject::sender());clients.removeOne(sender);sender-deleteLater(); } TcpClient.cpp #include tcpclient.h #include QDebugTcpClient::TcpClient(QObject *parent) : QObject(parent) { }void TcpClient::connectToServer(QString ipAddress, int port) {socket new QTcpSocket(this);connect(socket, SIGNAL(connected()), this, SLOT(onConnected()));connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));socket-connectToHost(ipAddress, port); }void TcpClient::sendData(QByteArray data) {if (socket socket-state() QAbstractSocket::ConnectedState)socket-write(data); }void TcpClient::onConnected() {qDebug() Connected!; }void TcpClient::onDisconnected() {qDebug() Disconnected!; }void TcpClient::onError(QAbstractSocket::SocketError error) {qDebug() Error: socket-errorString(); }void TcpClient::onReadyRead() {qDebug() Received: socket-readAll(); } 3TCP客户端与TCP服务端通信打开main.cpp添加头文件以及创建TcpServer对象和TcpClient对象 main.cpp #include QCoreApplication #include tcpserver.h #include tcpclient.hint main(int argc, char *argv[]) {QCoreApplication a(argc, argv);//创建服务器对象TcpServer server;server.startServer();//创建客户端对象TcpClient client;client.connectToServer(127.0.0.1, 1234);//发送数据client.sendData(Hello from client!);return a.exec(); } 2、UDP通信 QT中实现UDP通信主要用到了以下类QUdpSocket、QHostAddress等 UdpServer是服务器端用于监听客户端发送的消息并回复同样的消息UdpClient是客户端用于向服务器发送一条消息并等待来自服务器的回复 示例代码 1创建两个头文件(UdpServer.h 和 UdpClient.h) UdpServer.h UdpServer继承自QObject该类包含私有成员变量QUdpSocket *udpSocketstart()函数用于启动服务器 #ifndef UDPSERVER_H #define UDPSERVER_H# 这两个头文件分别用于QObject基类和QUdpSocket类前者提供Qt对象模型所需的大量工具和服务后者用于UDP套接字支持 #include QObject #include QUdpSocketclass UdpServer : public QObject {Q_OBJECT public:explicit UdpServer(QObject *parent nullptr);void start();signals:private:QUdpSocket *udpSocket; };#endif // UDPSERVER_H UdpClient.h UdpClient同样继承自QObject包含一个QUdpSocket指针udpSocket和一个send()函数用于向服务器发送消息 #ifndef UDPCLIENT_H #define UDPCLIENT_H#include QObject #include QUdpSocketclass UdpClient : public QObject {Q_OBJECT public:explicit UdpClient(QObject *parent nullptr);void send(const QString message);signals:private:QUdpSocket *udpSocket; };#endif // UDPCLIENT_H 2创建两个源文件(UdpServer.cpp 和 UdpClient.cpp) UdpServer.cpp UdpServer的实现文件中首先在构造函数中创建了一个QUdpSocket对象udpSocket该对象用于通信start()函数则用于启动服务器包括绑定端口和建立接收数据的连接 bind()函数用于将UDP套接字与IP地址和端口绑定以便开始监听传入的消息readyRead信号是QUdpSocket类的一个信号这里使用connect()函数将其与另一个函数连接起来该函数会在每次接收到数据时执行 #include UdpServer.h #include QDebugUdpServer::UdpServer(QObject *parent) : QObject(parent) {udpSocket new QUdpSocket(this); }void UdpServer::start() {quint16 port 1234;if (udpSocket-bind(QHostAddress::AnyIPv4, port)){qDebug() UDP server is listening on port port;}else{qDebug() Failed to bind the UDP server to port port;}connect(udpSocket, QUdpSocket::readyRead, this, [this](){while (udpSocket-hasPendingDatagrams()){QByteArray datagram;datagram.resize(udpSocket-pendingDatagramSize());udpSocket-readDatagram(datagram.data(), datagram.size());qDebug() Received from client: datagram;}}); } UdpClient.cpp UdpClient的实现文件中同样在构造函数中创建了一个QUdpSocket对象udpSocket用于通信send()函数则用于向服务器发送消息 writeDatagram()函数用于将datagram指定的数据报发送到目标地址和端口qDebug()函数用于输出日志信息记录已发送至服务器的消息 #include UdpClient.h #include QDebugUdpClient::UdpClient(QObject *parent) : QObject(parent) {udpSocket new QUdpSocket(this); }void UdpClient::send(const QString message) {QByteArray datagram message.toUtf8();quint16 port 1234;QHostAddress address(127.0.0.1);udpSocket-writeDatagram(datagram, address, port);qDebug() Sent to server: message; }3UDP客户端与UDP服务端通信打开main.cpp添加头文件以及创建UdpServer对象和UdpClient对象 main.cpp 主函数主要是启动UdpServer和UdpClient并向服务器发送一条消息 udpServer和udpClient是用于运行这两个对象的实例send()函数发送了一条消息该消息会被服务器接收并回复同样的消息a.exec()函数开始Qt事件循环直到程序退出为止 #include QCoreApplication #include UdpServer.h #include UdpClient.hint main(int argc, char *argv[]) {QCoreApplication a(argc, argv);UdpServer udpServer;udpServer.start();UdpClient udpClient;udpClient.send(Hello from client!);return a.exec(); } 在Qt Creator中运行程序可以看到以下输出 UDP server is listening on port 1234 Sent to server: Hello from client! Received from client: Hello from client!这表明服务器成功接收了来自客户端的消息并回复了同样的消息
http://www.w-s-a.com/news/35301/

相关文章:

  • 无锡哪里有做网站的公司泸州网站建设公司
  • 怎么进行网站推广jsp可以做那些小网站
  • 懒人手机网站wordpress修改秒速
  • WordPress资讯网站用花生壳做网站
  • 关于营销方面的网站建设网站怎么克隆
  • 站长网seo综合查询工具电商公司简介
  • 全能网站建设教程广告制作公司需要什么设备
  • 汽车行业做网站网站改版seo建议
  • 建设职业注册中心网站photoshop属于什么软件
  • 公司网站展示有哪些wordpress工单
  • iis新建网站seo是做什么工作的
  • 临沂网站建设厂家做外贸的女生现状
  • 电子商务网站建设实践临沂做网站的
  • 网站职能建设论文做外贸都有哪些网站
  • 网站建设项目需求分析房地产网站源码
  • 网站充值提现公司账务怎么做中国能建设计公司网站
  • 网站信息资源建设包括哪些网站网站做维护
  • 网站性能优化的方法有哪些建设施工合同网站
  • 郑州建设企业网站山西省住房和城乡建设厅网站
  • 做网站的去哪找客户正规制作网站公司
  • 网站代理访问是什么意思外国优秀设计网站
  • 合肥个人建站模板网络技术服务有限公司
  • 做网站什么公司好dw企业网站开发教程
  • 怎么做自己的个人网站宝安网站设计哪家最好
  • 浩博建设集团网站站长网站统计
  • 电商网站开发视频seo排名优化方式方法
  • 宿迁市住房城乡建设局网站wordpress纯图片主题
  • 广州建设网站 公司湖北省建设信息网官网
  • 网站建立时间查询做百度移动网站优化排
  • 网站建设和运行费用qq恢复官方网站