QT笔记(1)——TCP通讯学习

        当用QT和halcon联合编程时候,Qt作为显示和表层处理工具,halcon作为算法程序用的时候,少不了与PLC通信,而PLC通信主流是工业以太网,也就是TCP/IP的通信,因为halcon主要应用的地方还是在工业上。这里将简单讲一下Qt的TCP通信。解释在注释里,运行图及源码如下:


代码服务器端头文件.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_pushButton_clicked()
{
     this->tcpSocket->write(ui->lineEdit->text().toLatin1());
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_2_clicked()
{
    tcpServerConnect();
}








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值