您的位置:首页 > 理论基础 > 计算机网络

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

2016-10-14 18:58 495 查看
        当用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_p
a916
ushButton_clicked()
{
this->tcpSocket->write(ui->lineEdit->text().toLatin1());
}

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

void Widget::on_pushButton_2_clicked()
{
tcpServerConnect();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: