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

 

posted @ 2016-03-29 11:41  高傲的monkey  阅读(2153)  评论(0编辑  收藏  举报