一:对TCP的原理简介:
二:Qt实行TCP机制():
(1):server.h:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include<QTcpServer> //监听套接字,只有服务器端才需要监听套接字 #include<QTcpSocket> //通信套接字 QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); QTcpServer *TcpServer; //创建监听套接字 QTcpSocket *Socket; //创建通信套接字 private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::Widget *ui; }; #endif // WIDGET_H
(2)main.cpp:
#include "server.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
(3):server.cpp
#include "server.h" #include "ui_widget.h" Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); setWindowTitle("服务端"); Socket = NULL; //为了close的时候程序仍然处于监听循环中/ TcpServer = new QTcpServer(this); //创建套接字. TcpServer->listen(QHostAddress::Any,6666); connect(TcpServer,&QTcpServer::newConnection, [=]() { Socket = TcpServer->nextPendingConnection(); //取出建立好的套接字. //取出IP地址,注意IPv4地址由32为的二进制数组成,我们看到的是用点十进制法表示的。 qint32 ip = Socket->peerAddress().toIPv4Address(); qint16 port = Socket->peerPort(); QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port); ui->readEdit->setText(temp); } ); connect(Socket,&QTcpSocket::readyRead, [=]() { //获取接受到的消息, QByteArray arry = Socket->readAll(); ui->readEdit->append(arry); } ); } Widget::~Widget() { delete ui; } //在布局中选中要转到槽的控件,自动生成槽函数.(而且在这个过程就已经指定信号 void Widget::on_pushButton_clicked() { if(Socket == NULL) //当没有客户端连接的时候,让其不执行下面的代码,如若执行程序就会自动终止. { return; //让其循环,在这里面. } //获取编辑区内容 QString str = ui->textEdit_2->toPlainText(); Socket->write(str.toUtf8().data());//发送获取到的内容. } void Widget::on_pushButton_2_clicked() { if(Socket == NULL) { return; } //主动和客户端端口断开连接. Socket->disconnectFromHost(); Socket->close(); Socket = NULL; }
(4):客户端:
custom.h
#ifndef CUSTOM_H #define CUSTOM_H #include <QWidget> #include <QTcpSocket> //通信套接字 QT_BEGIN_NAMESPACE namespace Ui { class Custom; } QT_END_NAMESPACE class Custom : public QWidget { Q_OBJECT public: Custom(QWidget *parent = nullptr); ~Custom(); QTcpSocket *Socket; private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); private: Ui::Custom *ui; }; #endif // CUSTOM_H
main.cpp
#include "custom.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Custom w; w.show(); return a.exec(); }
custom.cpp
#include "custom.h" #include "ui_custom.h" #include<QHostAddress> Custom::Custom(QWidget *parent): QWidget(parent),ui(new Ui::Custom) { ui->setupUi(this); Socket = NULL; Socket = new QTcpSocket(this); connect(Socket,&QTcpSocket::connected, [=]() { ui->read_TEXT->setText("成功和服务器建立好连接"); } ); connect(Socket,&QTcpSocket::readyRead, [=]() { //获取对方发送的内容 QByteArray array = Socket->readAll(); //追加到编辑区 ui->read_TEXT->append(array); } ); } Custom::~Custom() { delete ui; } void Custom::on_pushButton_clicked() { //获取服务器端口和IP地址 QString IP = ui->lineEdit_2->text(); qint16 port = ui->line_Ediy1->text().toInt(); //主动和服务器建立连接 Socket->connectToHost(QHostAddress(IP),port); } void Custom::on_pushButton_2_clicked() { //获取编辑框内容 QString str = ui->read_TEXT->toPlainText(); //发送数据 Socket->write(str.toUtf8().data()); } void Custom::on_pushButton_3_clicked() { //主动和对方断开连接 Socket->disconnectFromHost(); }
三:命名空间的使用: