disconnected()
Socket close信号,当socket执行close函数之后出发信号
connect(socket, SIGNAL(disconnected()),this,SLOT(disconnectedSlot()));//客户端断开连接
readyRead()
connect(socket, SIGNAL(readyRead()),this, SLOT(readyReadSlot())); //接收消息
QByteArray dataBa = socket->readAll();
TcpClient项目训练
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>
#include <QDateTime>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButtonConnect_clicked();
void disconnectedSlot();
void readyReadSlot();
void on_pushButtonSend_clicked();
private:
Ui::Widget *ui;
QTcpSocket *socket;
bool socketState;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
socket = new QTcpSocket;
socketState = false;
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnectedSlot())); //客户端断开连接
connect(socket, SIGNAL(readyRead()),this, SLOT(readyReadSlot())); //接收消息
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButtonConnect_clicked()
{
QString ipAddressStr = ui->lineEditAddress->text();
quint16 port = ui->lineEditPort->text().toInt();
if(!socketState)
{
socket->connectToHost(ipAddressStr, port);
if(socket->waitForConnected(3000))
{
qDebug() << "Connect2Server OK";
ui->pushButtonConnect->setText("断开连接");
socketState = true;
}
else
{
qDebug() << socket->errorString();
return;
}
}
else
{
socket->close();
ui->pushButtonConnect->setText("连接");
socketState = false;
}
}
void Widget::disconnectedSlot()
{
qDebug()<<"Socket close";
}
void Widget::readyReadSlot()
{
QByteArray dataBa = socket->readAll();
QString str = QString(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd")) +
":Recv\n" + str.fromLocal8Bit(dataBa.data()); //本地GBK转Unicode 解决乱码
ui->textBrowserRecv->append(QString(str)); //送显示
}
void Widget::on_pushButtonSend_clicked()
{
QString dataStr = ui->textEditSend->toPlainText();
//QByteArray data = dataStr.toUtf8(); //发送UTF编码数据
QByteArray data = dataStr.toLocal8Bit(); //发送本地字符(GBK)
if(socket->isOpen() && socket->isValid())
{
socket->write(data);
}
QString str = QString(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd")) +
":Recv\n" + str.fromLocal8Bit(data.data()); //本地GBK转Unicode 解决乱码
ui->textBrowserRecv->append(QString(str)); //送显示
}
End
本文分享自微信公众号 - 编程学习基地(LearnBase)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。