QT笔记(1)——TCP通讯学习
2016-10-14 18:58
495 查看
当用QT和halcon联合编程时候,Qt作为显示和表层处理工具,halcon作为算法程序用的时候,少不了与PLC通信,而PLC通信主流是工业以太网,也就是TCP/IP的通信,因为halcon主要应用的地方还是在工业上。这里将简单讲一下Qt的TCP通信。解释在注释里,运行图及源码如下:
代码服务器端头文件.h:
服务器端实现文件.cpp:
客户端头文件.h:
客户端实现文件.cpp文件:
代码服务器端头文件.h:
#include <QtNetwork> #include <QtNetwork/QTcpSocket> #include <QtNetwork/QTcpServer> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_pushButton_clicked(); void readData(); //接收来自服务端的数据 void sendData(); //发送消息 void acceptConnection();//接受客户端连接,必须客户端先发起连接,不然无法接收 private: Ui::Widget *ui; QTcpSocket *tcpSocket; QTcpServer *tcpServer; }; #endif // WIDGET_H
服务器端实现文件.cpp:
#include "widget.h" #include "ui_widget.h" #include <QMessageBox> #include <QIODevice> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->tcpServer = new QTcpServer(this); this->tcpSocket = new QTcpSocket(this); tcpServer->listen(QHostAddress::Any,6666); connect(tcpServer,SIGNAL(newConnection()), this,SLOT(acceptConnection())); } void Widget::acceptConnection() { //当有客户来访时将tcpSocket接受tcpServer建立的socket tcpSocket = tcpServer->nextPendingConnection(); connect(tcpSocket,SIGNAL(readyRead()), this,SLOT(readData())); //readyRead()信号在且只在有新数据到达时才会被发射 connect(tcpSocket,SIGNAL(disconnected()), tcpSocket,SLOT(deleteLater())); //disconnected()This signal is emitted when the socket has been disconnected //嵌套字断开时候用来销毁对象 } void Widget::readData() { QString datas =tcpSocket->readAll(); //接收字符串数据。 ui->lineEdit_2->setText(datas); //写在控件lineEdit里 } void Widget::sendData() { this->tcpSocket->write(ui->lineEdit->text().toLatin1()); } void Widget::on_pushButton_clicked() { sendData(); } Widget::~Widget() { delete ui; }
客户端头文件.h:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QMainWindow> #include <QtNetwork> #include <QtNetwork/QTcpSocket> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); protected: void tcpServerConnect();//建立服务器与客户端连接 private slots: void on_pushButton_clicked(); void readData(); //接收来自服务端的数据 void displayError(QAbstractSocket::SocketError); void on_pushButton_2_clicked(); private: Ui::Widget *ui; QTcpSocket *tcpSocket; QTcpServer *tcpServer; }; #endif // WIDGET_H
客户端实现文件.cpp文件:
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); tcpServerConnect(); } void Widget:: tcpServerConnect() { this->tcpSocket = new QTcpSocket(this); tcpSocket->abort(); tcpSocket->connectToHost("127.0.0.1",6666); //这里可以根据实际情况在用户界面上进行输入。 //这里我是采用程序启动就自访问服务端(也可根据实际情况采用手动连接手动输入服务端ip的方式。) connect(tcpSocket,SIGNAL(readyRead()), this,SLOT(readData())); //readyRead()表示服务端发送数据过来即发动信号,接着revData()进行处理。 } void Widget::readData() { QString datas = tcpSocket->readAll(); //接收字符串数据。 ui->lineEdit_2->setText(datas); //显示字符串数据。 } void Widget::displayError(QAbstractSocket::SocketError) { qDebug()<<tcpSocket->errorString(); tcpSocket->close(); } void Widget::on_p a916 ushButton_clicked() { this->tcpSocket->write(ui->lineEdit->text().toLatin1()); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_2_clicked() { tcpServerConnect(); }
相关文章推荐
- [学习笔记]Java网络编程之TCP通讯
- WCF 学习笔记(三)----构建双向通讯(tcp/http)
- C#学习笔记——TCP通讯
- [学习笔记]Java网络编程之TCP通讯
- Qt5--学习笔记-TCPsocket文件发送、接收
- SOCKET学习笔记1——TCP通讯
- TCP/IP学习笔记之六 --- ICMP: Internet控制报文协议
- [ASP.NET学习笔记之九]ASP.NET网络通讯
- TCP/IP学习笔记之八 --- 动态选路协议
- 学习笔记----图形界面程序Qt安装及其在2410-s上的移植
- TCP/IP学习笔记之十 --- IGMP:Internet组管理协议
- tcp助手类编程 暑期学习笔记(五)
- QT学习笔记- eclipse集成的QT designer(总结得不错)
- 学习笔记----图形界面程序Qt安装及其在2410-s上的移植
- Qt Model/View 学习笔记 (六)
- Qt Model/View 学习笔记 (五)
- Qt Model/View 学习笔记 (二):为使用Models与views热身
- Socket网络编程学习笔记(4):TCP消息边界处理
- TCP/IP学习笔记之三 --- IP: 网际协议
- tcp/ip学习第一步:网络中两台主机进行通讯,协议栈是如何处理数据报的.