Qt 中关于socket的读与写

69 篇文章 1 订阅
18 篇文章 0 订阅

大部分是转载

 服务器端:

// server 
    #include "testnet.h"   
    #include "ui_testnet.h"   
    #include <QtGui>   
    Testnet::Testnet(QWidget *parent) :   
        QMainWindow(parent),   
        ui(new Ui::Testnet)   
    {   
        ui->setupUi(this);   
        this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));   
        this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));   
    } 
    Testnet::~Testnet()   
    {   
        delete ui;   
    } 
    void Testnet::startTcpserver()   
    {   
        m_tcpServer = new QTcpServer(this);   
       
        m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip   
       
        connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。   
    } 
    void Testnet::newConnect()   
    {   
            m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket   
            connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽 
    }  
    void Testnet::readMessage() //读取信息   
    {  
        QByteArray qba= m_tcpSocket->readAll(); //读取   
        qDebug()<<qba;   
        QString ss=QVariant(qba).toString();   
        ui->textEdit_rec->setText(ss);   
       
    } 
    void Testnet::sendMessage() //发送信息   
    {   
        QString strMesg= ui->lineEdit_sendmessage->text();   
        qDebug()<<strMesg;   
        m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送   
    }   
 
客户端:
// client 
    #include "testnet_c.h"   
    #include "ui_testnet_c.h"   
    testnet_c::testnet_c(QWidget *parent) :   
        QMainWindow(parent),   
        ui(new Ui::testnet_c)   
    {   
        ui->setupUi(this);   
       
        this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));   
        this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));   
    }   
       
    testnet_c::~testnet_c()   
    {   
        delete ui;   
    }   
 
    void testnet_c::connectServer()   
    {   
        m_tcpSocket = new QTcpSocket(this);   
        m_tcpSocket->abort();   
        m_tcpSocket->connectToHost("192.168.1.178",19999);   
       
        connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));   
    } 
    void testnet_c::readMesg() //读取信息   
    {   
       QByteArray qba =   m_tcpSocket->readAll();   
       
       ui->textEdit_recmesg->clear();   
       
       qDebug()<<qba;   
       QString ss=QVariant(qba).toString();   
       ui->textEdit_recmesg->setText(ss);   
    } 
    void testnet_c::sendMesg() //发送信息   
    {   
        QString ss= ui->lineEdit_sendmesg->text();   
        m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));   
        ui->lineEdit_sendmesg->clear();   
    }  

 
可见,在通信过程中,读信息需要检测信号,而写信息却可以直接进行 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值