首页 > 学院 > 开发设计 > 正文

Qt5 TcpSocket 客户端/服务器 通讯实例

2019-11-08 00:49:48
字体:
来源:转载
供稿:网友
Qt  Tcp   客户端 /服务端通信实例   客户端 QTcpSocket[html] view plain copy 在CODE上查看代码片   Client.PRo  #-------------------------------------------------  #  # Project created by QtCreator 2016-03-23T21:15:18  #  #-------------------------------------------------      QT       += core gui      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets      TARGET = Client  TEMPLATE = app          SOURCES += main.cpp/          mainwindow.cpp      HEADERS  += mainwindow.h      FORMS    += mainwindow.ui  QT +=network  mainwindow.h[html] view%20plain copy #ifndef MAINWINDOW_H  #define MAINWINDOW_H      #include <QMainWindow>  #include<QTcpSocket>  #include<QString>  #include<QDataStream>  #include<QByteArray>  namespace Ui {  class MainWindow;  }      class MainWindow : public QMainWindow  {      Q_OBJECT      public:      explicit MainWindow(QWidget *parent = 0);      ~MainWindow();  private slots:      void on_pushButto_conn_clicked();      private:      QTcpSocket * socket;  private slots:      void connected();      void on_pushButton_discon_clicked();      void readyread();  private:      Ui::MainWindow *ui;  };      #endif // MAINWINDOW_H  mainwindow.cpp[html] view%20plain copy #include "mainwindow.h"  #include "ui_mainwindow.h"  #include<QHostAddress>  #include<QMessageBox>  MainWindow::MainWindow(QWidget *parent) :      QMainWindow(parent),      ui(new Ui::MainWindow)  {      ui->setupUi(this);      this->socket=new QTcpSocket(this);  }      MainWindow::~MainWindow()  {      delete ui;  }      void MainWindow::on_pushButto_conn_clicked()  {      this->socket->connectToHost("127.0.0.1",80100,QTcpSocket::ReadWrite);      connect(this->socket,SIGNAL(connected()),this,SLOT(connected()));      }  void MainWindow::connected()  {      QMessageBox::about(this,"提示","连接成功");      connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyread()));      }      void MainWindow::on_pushButton_discon_clicked()  {      this->socket->close();  }  void MainWindow::readyread()  {      QMessageBox::about(this,"提示","准备读取");      QByteArray arr=this->socket->readAll();      QDataStream * dst=new QDataStream(&arr,QIODevice::ReadOnly);/******重点******/      QString str1;      QString str2;      (*dst)>>str1>>str2;      this->ui->textBrowser->setText(str1+str2);      QMessageBox::about(this,"x",str1+str2);  }  main.cpp[html] view%20plain copy #include "mainwindow.h"  #include <Qapplication>      int main(int argc, char *argv[])  {      QApplication a(argc, argv);      MainWindow w;      w.show();          return a.exec();  }  服务端通信实例QTcpServerServer.pro[html] view%20plain copy #-------------------------------------------------  #  # Project created by QtCreator 2016-03-23T20:48:06  #  #-------------------------------------------------      QT       += core gui      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets      TARGET = Server  TEMPLATE = app          SOURCES += main.cpp/          mainwindow.cpp      HEADERS  += mainwindow.h      FORMS    += mainwindow.ui  QT      +=network  mainwindow.h[html] view%20plain copy #ifndef MAINWINDOW_H  #define MAINWINDOW_H      #include <QMainWindow>  #include<QTcpServer>  #include<QTcpSocket>  namespace Ui {  class MainWindow;  }      class MainWindow : public QMainWindow  {      Q_OBJECT      public:      explicit MainWindow(QWidget *parent = 0);      ~MainWindow();      QTcpServer * server;      QTcpSocket * socket;  private slots:      void newConnection();      void ReceiveData();      void on_pushButton_2_clicked();          void on_pushButton_clicked();      private:      Ui::MainWindow *ui;  };      #endif // MAINWINDOW_H  mainwindow.cpp[html] view%20plain copy #include "mainwindow.h"  #include "ui_mainwindow.h"  #include<QTcpServer>  #include<QTcpSocket>  #include<QMessageBox>  #include<QDataStream>  #include<QString>  #include<QByteArray>  MainWindow::MainWindow(QWidget *parent) :      QMainWindow(parent),      ui(new Ui::MainWindow)  {      ui->setupUi(this);      this->socket=0;      this->server=new QTcpServer(this);      this->server->listen(QHostAddress::Any,80100);       QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newConnection()));  }      MainWindow::~MainWindow()  {      delete ui;  }  void MainWindow::newConnection(){      this->socket=this->server->nextPendingConnection();      QMessageBox::about(this,"提示","有新的连接!");      connect(this->socket,SIGNAL(readyRead()),this,SLOT(ReceiveData()));              }  void MainWindow::ReceiveData(){         QByteArray arr=this->socket->readAll();     QDataStream dst(arr);     QString str1;     QString str2;     dst>>str1>>str2;     this->ui->browser->setText(str1+str2);  }      void MainWindow::on_pushButton_2_clicked()  {      QString str=this->ui->lineEdit->text();      QByteArray arr;      QDataStream dst(&arr,QIODevice::ReadWrite);/*QDataStream是继承于QIODevice的所以 必须要 设置读写权限*/      dst<<QString("信息")<<str;/*这里一定要设置好数据类型*/      this->socket->write(arr);  }      void MainWindow::on_pushButton_clicked()  {      this->socket->close();  }  main.cpp[html] view%20plain copy 派生到我的代码片#include "mainwindow.h"  #include <QApplication>      int main(int argc, char *argv[])  {      QApplication a(argc, argv);      MainWindow w;      w.show();          return a.exec();  }  
上一篇:UVa-1225

下一篇:八数码难题

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表