qt tcp socket简单的通信程序

  1. socket通信分为server端与client端,基于tcp的需要首先建立server-client的连接,然后才能通信。
  2. 客户端程序如下:
  3. 在QT上建立一个widget界面程序,在界面中添加一个button和LineEdite,分别命名为sendButton、inputLine;LineEdit是客户端输入,点击button发送输入内容。
  4. 在工程文件中加入 QT +=network
  5. 客户端的mywidget.h
  6. #ifndef MYWIDGET_H
    #define MYWIDGET_H


    #include <QWidget>
    #include <QtNetwork>


    namespace Ui {
    class myWidget;
    }


    class myWidget : public QWidget
    {
        Q_OBJECT
        
    public:
        explicit myWidget(QWidget *parent = 0);
        ~myWidget();
        
    private:
        Ui::myWidget *ui;
        QTcpSocket *client;
        const char *data;
    private slots:
        void senddata();
    };


    #endif // MYWIDGET_H
  7. 客户端的mywidget.cpp
  8. :#include "mywidget.h"
    #include "ui_mywidget.h"


    myWidget::myWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::myWidget)
    {
        ui->setupUi(this);
        client = new QTcpSocket(this);
        client->connectToHost(QHostAddress("10.9.3.95"),5000);
        connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(senddata()));
    }


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


    void myWidget::senddata()
    {
        data=ui->inputLine->text().toStdString().c_str();
        client->write(data);
        ui->inputLine->setText("send ok!");
    }
  9. 服务端:服务端本程序中只用来接收客户端发送来的消息,在server的界面中添加一个label用来提示是否有客户端连接;添加一个LineEdit用来输出服务端接收到的
  10. 消息。在工程中添加QT  +=network
  11. 服务端程序:


  12. #ifndef MYWIDGET_H
    #define MYWIDGET_H


    #include <QWidget>
    #include <QTcpServer>
    #include <QTcpSocket>


    namespace Ui {
    class mywidget;
    }


    class mywidget : public QWidget
    {
        Q_OBJECT
        
    public:
        explicit mywidget(QWidget *parent = 0);
        ~mywidget();
        
    private:
        Ui::mywidget *ui;
        QTcpServer *server;
        QTcpSocket *clientConnection;
        char recbuf[1024];
    private slots:
        void acceptConnection();
        void readClient();
    };


    #endif // MYWIDGET_H
  13. #include "mywidget.h"
    #include "ui_mywidget.h"


    mywidget::mywidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::mywidget)
    {
        ui->setupUi(this);
        server=new QTcpServer();
        clientConnection=new QTcpSocket();
        server->listen(QHostAddress::Any,5000);
        connect(server,SIGNAL(newConnection()),this,SLOT(acceptConnection()));




    }


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


    void mywidget::acceptConnection()
    {
        clientConnection=server->nextPendingConnection();
        ui->label->setText("connect....");
        connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readClient()));
    }


    void mywidget::readClient()
    {
        memset(recbuf,0,sizeof(recbuf));
        clientConnection->read(recbuf,1024);
        ui->recLine->clear();
        ui->recLine->setText(recbuf);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值