tcp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// -------------- widget.h: #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTcpServer> //服务器 #include <QTcpSocket> // 套接字 发送的消息 namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); QTcpServer* _tcpServer = NULL; QTcpSocket* _tcpSocket = NULL; private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::Widget *ui; }; #endif // WIDGET_H // -------------- widget.cpp: #include "widget.h" #include "ui_widget.h" #include <QHostAddress> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); setWindowTitle("服务端:8888"); //_tcpSocket = new QTcpSocket(this); _tcpServer = new QTcpServer(this); _tcpServer->listen(QHostAddress::Any,8888); // 服务第的ip和端口 connect(_tcpServer, &QTcpServer::newConnection, [=](){ // 有人连接时触发 _tcpSocket = _tcpServer->nextPendingConnection(); // 获取对方套接字,包含对方的基础信息 QString ip = _tcpSocket->peerAddress().toString(); // 通过套接字获取对方的iP qint16 port = _tcpSocket->peerPort(); // 通过套接字获取对方的端口 QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port); ui->textEdit->setText(temp); connect(_tcpSocket,&QTcpSocket::readyRead,[=](){ // 接收消息,对方连接我成功后方可接收 QByteArray array = _tcpSocket->readAll(); ui->textEdit->append(array); }); }); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() //给客户端发数据 { if(_tcpSocket == NULL) return; QString str = ui->textEdit_2->toPlainText(); _tcpSocket->write(str.toUtf8().data()); } void Widget::on_pushButton_2_clicked() // 和客户端断开连接 { if(_tcpSocket == NULL) return; _tcpSocket->disconnectFromHost(); _tcpSocket->close(); _tcpSocket = NULL; } // -------------- socket.h: #ifndef SOCKET_H #define SOCKET_H #include <QWidget> #include <QTcpSocket> namespace Ui { class socket; } class socket : public QWidget { Q_OBJECT public: explicit socket(QWidget *parent = 0); ~socket(); QTcpSocket* _tcpSocket = NULL; private slots: void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); void on_pushButton_clicked(); private: Ui::socket *ui; }; #endif // SOCKET_H // -------------- socket.cpp: #include "socket.h" #include "ui_socket.h" #include <QHostAddress> socket::socket(QWidget *parent) : QWidget(parent), ui(new Ui::socket) { ui->setupUi(this); _tcpSocket = new QTcpSocket(this); // 分配空间,指定父对象 connect(_tcpSocket,&QTcpSocket::connected,[=](){ // 和服务器连接时触发 ui->textEdit->setText("成功和服务器连接"); connect(_tcpSocket,&QTcpSocket::readyRead,[=](){ // 接收消息,我连接对方成功后方可接收 QByteArray array = _tcpSocket->readAll(); ui->textEdit->append(array); }); }); } socket::~socket() { delete ui; } void socket::on_pushButton_clicked() // 连接服务器 { QString ip = ui->lineEdit->text(); qint16 port = ui->lineEdit_2->text().toInt(); _tcpSocket->connectToHost(QHostAddress(ip),port); } void socket::on_pushButton_2_clicked() // 发送数据 { if(_tcpSocket == NULL) return; QString str = ui->textEdit_2->toPlainText(); _tcpSocket->write(str.toUtf8().data()); } void socket::on_pushButton_3_clicked() // 断开连接 { if(_tcpSocket == NULL) return; _tcpSocket->disconnectFromHost(); _tcpSocket->close(); _tcpSocket = NULL; }基础示例
标签:Widget,tcpSocket,clicked,qt,网络协议,ui,include,socket From: https://www.cnblogs.com/fxw1/p/16633479.html