zoukankan      html  css  js  c++  java
  • Qt的Socket数据通讯的一个样例。

    QTcpServer类 用来侦听port ,获取QTcpSocket.

    QTcpSocket有  connected的信号(已经连接),还有readyread()信号,表示已经有数据发过来了。准备读取。

      若要关闭 当前连接仅仅须要 调用 qtcpsocket::close();就关闭了当前连接

    以下有两个样例

       server端

    用的是控制台程序(QT) 当用户 发送数据过来 就cout显示。然后就write一个 I Love You的字符串 返回到client。然后close断开连接

    client

      用的书图形界面。一个输入框 输入数据 然后发送,最后 QMessagebox显示server返回消息

    =======================

    server端(三个文件)

    #ifndef MYSERVER_H
    #define MYSERVER_H
    #include<QTcpSocket>
    #include<iostream>
    #include <QObject>
    #include<QTcpServer>
    class myserver : public QTcpServer
    {
        Q_OBJECT
    public:
        QTcpSocket * socket;
        QTcpServer *server;
        myserver();
    private slots:
        void getData();
        void newconnectslot();
    };
    #endif // MYSERVER_H
    
    
    #include "myserver.h"
    #include<QByteArray>
    #include<QString>
    #include<QDataStream>
    myserver::myserver()
    {
        this->socket=new QTcpSocket;
        this->server=new QTcpServer;
    
    
        if(this->server->listen(QHostAddress::Any,520))
        {
            std::cout<<"bind port 520 successful."<<std::endl;
        }else
        {
            std::cout<<"bind port 520 failed."<<std::endl;
        }
         QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newconnectslot()));
    
    
    }
    
    
    void myserver::newconnectslot()
    {
        this->socket=this->server->nextPendingConnection();
        connect(this->socket,SIGNAL(readyRead()),this,SLOT(getData()));
    
    
    }
    void myserver::getData()
    {
        QByteArray by=this->socket->readAll();
        QDataStream ds(by);
        QString x;
        ds>>x;
        QByteArray ba = x.toLatin1();
    
    
        char * p=ba.data();
        std::cout<<p<<std::endl;
        socket->write("I love you");//返回给client
        this->socket->close();//断开连接
    }
    
    
    #include <QCoreApplication>
    #include<iostream>
    #include"myserver.h"
    #include<QHostAddress>
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        std::cout<<"--Server initialized By HanHan--"<<std::endl;
        myserver *server=new myserver;
        return a.exec();
    }
    
    
    
    
    
    client(三个文件
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    
    #include <QMainWindow>
    #include<QString>
    #include<QByteArray>
    #include<QDataStream>
    
    
    #include<QTcpSocket>
    namespace Ui {
    class MainWindow;
    }
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    
    public:
        QTcpSocket * socket;
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
    private slots:
        void connnectslot();
        void on_btn_send_clicked();
        void readyslot();
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    
    #endif // MAINWINDOW_H
    
    
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QHostAddress>
    #include<QMessageBox>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        this->socket=new QTcpSocket;
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_btn_send_clicked()
    {
    
    
        QHostAddress address("127.0.0.1");
        this->socket->connectToHost(address,520);
        connect(this->socket,SIGNAL(connected()),this,SLOT(connnectslot()));
        connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyslot()));//接收发来的数据
    }
    void MainWindow::connnectslot()
    {
         QString data=this->ui->data_edit->toPlainText();
         QByteArray array;
         QDataStream ds(&array,QIODevice::WriteOnly);
         ds<<data;
         this->socket->write(array);
    
    
    }
    
    
    
    void MainWindow::readyslot()
    {
        QString x=this->socket->readAll();
        QMessageBox::about(this,"x",x);
    
    
    }
    #include "mainwindow.h"
    #include <QApplicatio>
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
    
        return a.exec();
    }
    
    
    
    
    

    执行截图:


    n
    
    
    
    

  • 相关阅读:
    for...in 循环对象原型链问题
    移动端表单禁用软键盘
    将一个普通数组映射为对象数组
    npm install命令详解
    Elasticsearch High Level REST Client
    Guava: Google Core Libraries for Java
    Java Interview Programming Questions
    2017 OWASP TOP 10
    17 Popular Java Frameworks in 2018
    10 Popular PHP frameworks in 2019
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7091277.html
Copyright © 2011-2022 走看看