QT写的tcp server例子

界面如下:在这里插入图片描述
源文件

#include <QDebug>
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    StartBtInit();
    EndBtInit();
    CleanBtInit();
    QuitBtInit();

    IpLableInit();
    PortLableInit();
    IpInputInit();
    PortInputInit();

    ScreenTextInit();

    TcpServerInit();

    connect(m_start_bt, SIGNAL(clicked()), this, SLOT(StartBtClicked()));
    connect(m_end_bt, SIGNAL(clicked()), this, SLOT(EndBtClicked()));
    connect(m_clean_bt, SIGNAL(clicked()), this, SLOT(CleanBtClicked()));
    connect(m_quit_bt, SIGNAL(clicked()), this, SLOT(QuitBtClicked()));

    connect(m_ip_input, SIGNAL(editingFinished()), this, SLOT(IpInputClicked()));
    connect(m_port_input, SIGNAL(editingFinished()), this, SLOT(PortInputClicked()));

    setWindowTitle(tr("M device"));
}

void Dialog::TcpServerInit()
{
    m_tcp_server = new QTcpServer(this);
    m_tcp_socket_conn = new QTcpSocket();

    //connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(OnConnected()));
    //connect(m_tcp_server, SIGNAL(disconnected()), SLOT(OnDisconnected()));
    //connect(m_tcp_server, SIGNAL(readyRead()), SLOT(OnReadyRead()));

}

void Dialog::SendMessage()
{
    qDebug() <<"---SendMessage--";
}

void Dialog::OnConnected()
{
    qDebug() <<"---OnConnected---";
    m_tcp_socket_conn = m_tcp_server->nextPendingConnection();

    //connect(m_tcp_socket_conn, SIGNAL(newConnection()), this, SLOT(OnConnected()));
    connect(m_tcp_socket_conn, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
    connect(m_tcp_socket_conn, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
}

void Dialog::OnDisconnected()
{
    qDebug() <<"---OnDisconnected---";
}

void Dialog::OnReadyRead()
{
    //qDebug() <<"---OnReadyRead---";
    m_screen_text->moveCursor(QTextCursor::Start);
    QTextCodec * codec = QTextCodec::codecForName("gb2312");
    m_screen_text->textCursor().insertText(codec->toUnicode(m_tcp_socket_conn->readAll()));
    m_screen_text->textCursor().insertText("\r\n");
}

void Dialog::ScreenTextInit()
{
    m_screen_text = new QTextBrowser();
    m_screen_text->setParent(this);
    m_screen_text->setTextColor(Qt::red);
    m_screen_text->setTextBackgroundColor(Qt::darkGreen);
    m_screen_text->setGeometry(100,200,500,300);
}

void Dialog::IpInputClicked()
{

}

void Dialog::PortInputClicked()
{

}

void Dialog::IpLableInit()
{

}

void Dialog::PortLableInit()
{

}
void Dialog::IpInputInit()
{
    QFont font;
    font.setFamily("宋体");
    font.setPixelSize(20);

    m_ip_input = new QLineEdit();
    m_ip_input->setParent(this);
    m_ip_input->setFont(font);
    m_ip_input->setPlaceholderText("please input ip");
    m_ip_input->setGeometry(100,50,200,30);

    m_ip_input->setText("192.168.10.124");
}

void Dialog::PortInputInit()
{
    QFont font;
    font.setFamily("宋体");
    font.setPixelSize(20);

    m_port_input = new QLineEdit();
    m_port_input->setParent(this);
    m_port_input->setFont(font);
    m_port_input->setPlaceholderText("please input port");
    m_port_input->setGeometry(400,50,200,30);

    m_port_input->setText("7700");
}

void Dialog::StartBtClicked()
{
    m_screen_text->moveCursor(QTextCursor::Start);
    m_screen_text->textCursor().insertText("start test!!!\r\n");

    if(m_tcp_server->isListening()){
        m_tcp_server->close();
    }
    if(!m_tcp_server->listen(QHostAddress(m_ip_input->text()),m_port_input->text().toInt())) {
        qDebug() << m_tcp_server->errorString();
        close();
    }

    connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(OnConnected()));
}

void Dialog::EndBtClicked()
{
    m_screen_text->moveCursor(QTextCursor::Start);
    m_screen_text->textCursor().insertText("end test!!!\r\n");

    if(m_tcp_server->isListening()) {
        m_tcp_server->close();
    }
    if(m_tcp_socket_conn){
        m_tcp_socket_conn->close();
    }

}

void Dialog:: CleanBtClicked()
{
    m_screen_text->clear();
}

void Dialog::QuitBtClicked()
{
    if(m_tcp_server->isListening()){
        m_tcp_server->close();
    }
}

void Dialog::StartBtInit()
{
    m_start_bt = new QPushButton();
    m_start_bt->setParent(this);
    m_start_bt->setText("start test");
    m_start_bt->move(100,100);
    m_start_bt->resize(80,50);
}

void Dialog::EndBtInit()
{
    m_end_bt = new QPushButton();
    m_end_bt->setParent(this);
    m_end_bt->setText("end test");
    m_end_bt->move(200,100);
    m_end_bt->resize(80,50);
}

void Dialog::CleanBtInit()
{
    m_clean_bt = new QPushButton();
    m_clean_bt->setParent(this);
    m_clean_bt->setText("clean test");
    m_clean_bt->move(300,100);
    m_clean_bt->resize(80,50);
}

void Dialog::QuitBtInit()
{
    m_quit_bt = new QPushButton();
    m_quit_bt->setParent(this);
    m_quit_bt->setText("quit test");
    m_quit_bt->move(400,100);
    m_quit_bt->resize(80,50);
}

Dialog::~Dialog()
{
    if(m_start_bt){
        delete(m_start_bt);
        m_start_bt = NULL;
    }
    if(m_end_bt){
        delete(m_end_bt);
        m_end_bt = NULL;
    }
    if(m_clean_bt){
        delete(m_clean_bt);
        m_clean_bt = NULL;
    }
    if(m_quit_bt){
        delete(m_quit_bt);
        m_quit_bt = NULL;
    }

    delete ui;
}

头文件


```cpp
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTextBrowser>
#include <QtNetwork>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    
    void StartBtInit();
    void EndBtInit();
    void CleanBtInit();
    void QuitBtInit();
    void IpLableInit();
    void PortLableInit();
    void IpInputInit();
    void PortInputInit();
    void ScreenTextInit();

    void TcpServerInit();
private slots:
    void StartBtClicked();
    void EndBtClicked();
    void CleanBtClicked();
    void QuitBtClicked();
    void IpInputClicked();
    void PortInputClicked();

    void SendMessage();
    void OnConnected();
    void OnDisconnected();
    void OnReadyRead();
private:
    Ui::Dialog *ui;
    QPushButton *m_start_bt;
    QPushButton *m_end_bt;
    QPushButton *m_clean_bt;
    QPushButton *m_quit_bt;
    QLabel *m_ip_label;
    QLabel *m_port_label;
    QLineEdit *m_ip_input;
    QLineEdit *m_port_input;
    QTextBrowser *m_screen_text;

    QTcpServer *m_tcp_server;
    QTcpSocket *m_tcp_socket_conn;
};

#endif // DIALOG_H


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小白鱼儿_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值