QTcpServer和QTcpSocket的使用

最近产品要做一个PC端与PAD端通信收发信息的功能。研究了一下Qt的实现。看到一个很好的帖子,简单易懂。仿照写了一个例子,记录下来备忘。

  • 客户端

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void connectServer();
    void slotReadMessage();
    void slotSendMessage();

private:
    Ui::MainWindow *ui;

    QTcpSocket *m_tcpSocket;
};

#endif // MAINWINDOW_H

源文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_tcpSocket(0)
{
    ui->setupUi(this);

    ui->IpLineEdit->setText("192.168.1.179");

    connect(ui->connectBtn, SIGNAL(clicked()), this, SLOT(connectServer()));
    connect(ui->sendMsgBtn, SIGNAL(clicked()), this, SLOT(slotSendMessage()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::connectServer()
{
    m_tcpSocket = new QTcpSocket(this);
    m_tcpSocket->abort();
    QString ip = ui->IpLineEdit->text().trimmed();
    qDebug()<<"ip="<<ip;
    m_tcpSocket->connectToHost(ip, 19999);

    // 当 QTcpSocket 上有新数据可读时,则会触发信号 readyRead(), 读取信息
    connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotReadMessage()));
}

void MainWindow::slotReadMessage() // 读取信息
{
    QByteArray contents = m_tcpSocket->readAll();
    ui->receiveMsgTextEdit->clear();
    ui->receiveMsgTextEdit->setText(QString(contents));
}

void MainWindow::slotSendMessage() // 发送信息
{
    QString msg = ui->sendMsgLineEdit->text();
    qint64 ret = m_tcpSocket->write(msg.toStdString().c_str(), strlen(msg.toStdString().c_str()));
    if (ret < 0)
    {
        qDebug()<<"failed. error!";
        return;
    }
    ui->sendMsgLineEdit->clear();
}

main函数:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
  • 服务端

    头文件:

#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();

private slots:
    void startTcpserver();
    void slotCreateNewConnect();
    void slotReadMessage();
    void slotSendMessage();

private:
    Ui::MainWindow *ui;

    QTcpServer *m_tcpServer;
    QTcpSocket *m_tcpSocket;
};

#endif // MAINWINDOW_H

源文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(startTcpserver()));
    connect(ui->sendMsgBtn, SIGNAL(clicked()), this, SLOT(slotSendMessage()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::startTcpserver()
{
    m_tcpServer = new QTcpServer(this);

    // 监听任何连上19999端口的ip
    m_tcpServer->listen(QHostAddress::Any, 19999);

    // 新连接信号触发,调用 newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
    connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(slotCreateNewConnect()));
}

void MainWindow::slotCreateNewConnect()
{
    m_tcpSocket = m_tcpServer->nextPendingConnection(); // 得到每个连进来的socket
    // 有可读的信息,触发读信息的槽函数
    connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotReadMessage()));
}

void MainWindow::slotReadMessage() // 读取信息
{
    QByteArray contents = m_tcpSocket->readAll(); // 读取
    ui->textEdit_rec->clear();
    ui->textEdit_rec->setText(QString(contents));
}

void MainWindow::slotSendMessage() // 发送信息
{
    QString msg = ui->lineEdit_sendmessage->text();
    m_tcpSocket->write(msg.toStdString().c_str(), strlen(msg.toStdString().c_str())); // 发送
}

main函数:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值