zoukankan      html  css  js  c++  java
  • QT socket相关

    #include<QtNetwork/QTcpSocket>
    #include<QtNetwork/QTcpServer>

    1.服务器端

    void About::init_tcp()
    {
        //server
        this->tcpServer = new QTcpServer(this);
        this->tcpSocket = new QTcpSocket(this);
        if(!tcpServer->listen(QHostAddress::Any,6666))
       {
          qDebug()<<tcpServer->errorString();
          close();
          return;
        }
        connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
    }
    void About::acceptConnection()
    
    {
      tcpSocket = tcpServer->nextPendingConnection();
      qDebug() << "a client connect!!";
      connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(revData()));
    }
    
    void About::tcp_write(const char* data)
    {
        tcpSocket->write(data);
    }

    2. 客户端,加入自动重连

    void About::init_tcp()
    {
        //client
        QString ip = getIP();
        this->tcpSocket = new QTcpSocket(this);
        tcpSocket->abort();
        tcpSocket->connectToHost(ip,6666);
        connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(revData()));
    
        connect(tcpSocket, SIGNAL(connected()), this, SLOT(OnSocketConnected()));
        connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(OnSocketDisconnected()));
        connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(OnSocketError(QAbstractSocket::SocketError)));
    
        connect(&m_timer, SIGNAL(timeout()), this, SLOT(ConnectServer()));
        m_timer.setInterval(3000);
        m_timer.start();
    }
    
    QString About::getIP()
    {
        QString local_ip;
        QString str;
        QList<QHostAddress> list = QNetworkInterface::allAddresses();
            foreach (QHostAddress address, list)
            {
                if(address.protocol() == QAbstractSocket::IPv4Protocol)
                {
                    //IPv4地址
                    if (address.toString().contains("127.0."))
                    {
                        continue;
                    }
                    local_ip = address.toString();
                }
            }
            if (local_ip == "127.0.0.1")
            {
                qDebug() << "get local ip fail";
                return 0;
            }
            else
            {
                return local_ip;
            }
    }
    
    void About::OnSocketConnected()
    {
        qDebug() << "connected to server";
        m_bServerConnected = true;
    }
    
    void About::OnSocketDisconnected()
    {
        qDebug() <<"Server disconnected";
        m_bServerConnected = false;
    }
    
    void About::OnSocketError(QAbstractSocket::SocketError error)
    {
        //emit ShowStatus(m_pTcpSocket->errorString());
        qDebug() << tcpSocket->errorString();
    }
    
    void About::ConnectServer()
    {
        if(!m_bServerConnected)
        {
            QString ip = getIP();
            tcpSocket->connectToHost(ip, 6666);
            //m_pTcpSocket->waitForConnected(2000);//如果调用这句,界面会卡死
        }
    }
    void About::revData()
    {
       //server
        QString datas = tcpSocket->readAll();
        qDebug()<<datas;
    }
    void About::tcp_write(const char* data)
    {
        tcpSocket->write(data);
    }
  • 相关阅读:
    自定义View的ToolBar布局报错Error:(2) No resource identifier found for attribute 'context' in package 'c
    在学git之主分支 branch
    获取发布版SHA1
    关于开启线程与UI的操作
    播放音频和视频(VideoView控件)
    通知栏Notification的应用
    Android 真机调式 Installation failed with message 远程主机强迫关闭了一个现有的连接。. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing. WA
    运行程序申请危险权限
    mysql乐观锁总结和实践
    Nginx配置文件nginx.conf中文详解
  • 原文地址:https://www.cnblogs.com/karappo/p/5994303.html
Copyright © 2011-2022 走看看