zoukankan      html  css  js  c++  java
  • qt中的tcp编程

    server

    1.server.h
    #define DIALOG_H
    
    #include <QDialog>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include<QHostAddress>
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_pushButton_2_clicked();
    
    private:
        Ui::Dialog *ui;
        QTcpServer *p;
        QTcpSocket *newp;//产生的新socket
        QHostAddress *hp;
    public slots:
        void apprecvconnect();//处理连接函数
        void getmsg();
    };
    
    #endif // DIALOG_H

    server.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include<QString>
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        p = new QTcpServer();
    
       // newp = new QTcpSocket();
        hp = new QHostAddress("192.168.1.30");
        connect(p,SIGNAL(newConnection()),this,SLOT(apprecvconnect()));
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
        p->listen(*hp,6000);
    
    }
    void Dialog::apprecvconnect()
    {
        newp = p->nextPendingConnection();
        connect(newp,SIGNAL(readyRead()),this,SLOT(getmsg()));
    
    }
    void Dialog::getmsg()
    {
    QByteArray tmp = newp->readAll();
    QString tmp2(tmp);
    ui->label_2->setText(tmp2);
    }
    
    void Dialog::on_pushButton_2_clicked()
    {
        QString tmp = ui->lineEdit->text();
    newp->write(tmp.toLatin1());
    }

    client.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include<QTcpServer>
    #include<QTcpSocket>
    #include<QHostAddress>
    #include<QMessageBox>
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_pushButton_clicked();
        void on_pushButton_2_clicked();
    
    public slots:
        void sett();
        void getmsg();
    
    private:
        Ui::Dialog *ui;
        QTcpSocket *p;
        QHostAddress *hp;
    };
    
    #endif // DIALOG_H

    sever.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        p= new QTcpSocket();
        hp = new QHostAddress("192.168.1.30");
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
        p->connectToHost(*hp,6000);
        connect(p,SIGNAL(connected()),this,SLOT(sett()));
    }
    void Dialog::sett()
    {
        QMessageBox::about(this,"about","connect success");
        connect(p,SIGNAL(readyRead()),this,SLOT(getmsg()));
    
    }
    void Dialog::getmsg()
    {
        QByteArray tmp = p->readAll();
        QString tmp2(tmp);
        ui->label_2->setText(tmp2);
    }
    void Dialog::on_pushButton_2_clicked()
    {
        QString tmp = ui->lineEdit->text();
    p->write(tmp.toLatin1());
    }

    ui.h

    #ifndef UI_DIALOG_H
    #define UI_DIALOG_H
    
    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QDialog>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QLineEdit>
    #include <QtWidgets/QPushButton>
    
    QT_BEGIN_NAMESPACE
    
    class Ui_Dialog
    {
    public:
        QPushButton *pushButton;
        QPushButton *pushButton_2;
        QLabel *label;
        QLineEdit *lineEdit;
        QLabel *label_2;
    
        void setupUi(QDialog *Dialog)
        {
            if (Dialog->objectName().isEmpty())
                Dialog->setObjectName(QStringLiteral("Dialog"));
            Dialog->resize(400, 300);
            pushButton = new QPushButton(Dialog);
            pushButton->setObjectName(QStringLiteral("pushButton"));
            pushButton->setGeometry(QRect(250, 70, 99, 27));
            pushButton_2 = new QPushButton(Dialog);
            pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
            pushButton_2->setGeometry(QRect(250, 170, 99, 27));
            label = new QLabel(Dialog);
            label->setObjectName(QStringLiteral("label"));
            label->setGeometry(QRect(120, 30, 181, 17));
            lineEdit = new QLineEdit(Dialog);
            lineEdit->setObjectName(QStringLiteral("lineEdit"));
            lineEdit->setGeometry(QRect(60, 170, 141, 27));
            label_2 = new QLabel(Dialog);
            label_2->setObjectName(QStringLiteral("label_2"));
            label_2->setGeometry(QRect(50, 70, 181, 31));
    
            retranslateUi(Dialog);
    
            QMetaObject::connectSlotsByName(Dialog);
        } // setupUi
    
        void retranslateUi(QDialog *Dialog)
        {
            Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0));
            pushButton->setText(QApplication::translate("Dialog", "connect", 0));
            pushButton_2->setText(QApplication::translate("Dialog", "send", 0));
            label->setText(QApplication::translate("Dialog", "client", 0));
            label_2->setText(QString());
        } // retranslateUi
    
    };
    
    namespace Ui {
        class Dialog: public Ui_Dialog {};
    } // namespace Ui
    
    QT_END_NAMESPACE
    
    #endif // UI_DIALOG_H
  • 相关阅读:
    【动态规划】数的划分
    【动态规划】开心的小明
    【动态规划】回文字符串
    【动态规划】skiing_深度搜索_动态规划
    【动态规划】最大子串和
    JDBC中 execute 与 executeUpdate的区别
    poj 2449 Remmarguts' Date 求第k短路 Astar算法
    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer 最大生成树 lca
    hdu 2586 How far away ? 倍增求LCA
    acm模板生成
  • 原文地址:https://www.cnblogs.com/defen/p/5332348.html
Copyright © 2011-2022 走看看