Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How block/cancel specific IP adress
QtWS25 Last Chance

How block/cancel specific IP adress

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 342 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #1

    I have a QtTcpServer and more than one client .... I know the right ip adress to accept and would block or non accept other .... so I try in these way on my server side:

    /* in mainwindows */
    
    connect(server, SIGNAL(newConnection()), this, SLOT(new_con()));
    
    /* after */
    
    
    
    void MainWindow::new_con()
    {
    
    
        client = new QTcpSocket();
    
        client = server->nextPendingConnection();
        qDebug() << "connesso nuovo client...." << client->peerAddress().toString();
        QString conClient = client->peerAddress().toString();
    
    
        if(conClient == peerF){  connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket); qDebug() << "client F..." ;}
        if(conClient == peerBR){  connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket1); qDebug() << "client BR...." ;}
        if(conClient == peerB){  connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket2); qDebug() << "client B...." ;}
    
        if((conClient == peerF) || (conClient == peerBR) || (conClient == peerB)){
            connect(client, &QTcpSocket::disconnected,this, &MainWindow::discardSocket);
            connect(client, &QTcpSocket::errorOccurred, this, &MainWindow::sokError);
            connect(client, &QTcpSocket::connected, this, &MainWindow::sokConnSTATE);
            connect(client, &QTcpSocket::disconnected, this, &MainWindow::sokDiscSTATE);
            connect(client, &QTcpSocket::stateChanged, this, &MainWindow::sokState);
            client->setSocketOption(QAbstractSocket:: KeepAliveOption, 1);
        }
        else{client->deleteLater(); qDebug() << "discharge socket";}
        
        
        /*....... other stuff */
    }
    

    Any How when unappropriate ip adress try to connect my app crash .... appreciate any suggest for reach my goal.

    regards

    bkt

    Christian EhrlicherC 1 Reply Last reply
    0
    • gfxxG gfxx

      I have a QtTcpServer and more than one client .... I know the right ip adress to accept and would block or non accept other .... so I try in these way on my server side:

      /* in mainwindows */
      
      connect(server, SIGNAL(newConnection()), this, SLOT(new_con()));
      
      /* after */
      
      
      
      void MainWindow::new_con()
      {
      
      
          client = new QTcpSocket();
      
          client = server->nextPendingConnection();
          qDebug() << "connesso nuovo client...." << client->peerAddress().toString();
          QString conClient = client->peerAddress().toString();
      
      
          if(conClient == peerF){  connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket); qDebug() << "client F..." ;}
          if(conClient == peerBR){  connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket1); qDebug() << "client BR...." ;}
          if(conClient == peerB){  connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket2); qDebug() << "client B...." ;}
      
          if((conClient == peerF) || (conClient == peerBR) || (conClient == peerB)){
              connect(client, &QTcpSocket::disconnected,this, &MainWindow::discardSocket);
              connect(client, &QTcpSocket::errorOccurred, this, &MainWindow::sokError);
              connect(client, &QTcpSocket::connected, this, &MainWindow::sokConnSTATE);
              connect(client, &QTcpSocket::disconnected, this, &MainWindow::sokDiscSTATE);
              connect(client, &QTcpSocket::stateChanged, this, &MainWindow::sokState);
              client->setSocketOption(QAbstractSocket:: KeepAliveOption, 1);
          }
          else{client->deleteLater(); qDebug() << "discharge socket";}
          
          
          /*....... other stuff */
      }
      

      Any How when unappropriate ip adress try to connect my app crash .... appreciate any suggest for reach my goal.

      regards

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gfxx said in How block/cancel specific IP adress:

      client = new QTcpSocket();
      
      client = server->nextPendingConnection();
      

      Hello memleak...

      Any How when unappropriate ip adress try to connect my app crash

      Then use a debugger to find out where it crashes, your code above does not create a crash, just a memleak as shown above.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      gfxxG 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @gfxx said in How block/cancel specific IP adress:

        client = new QTcpSocket();
        
        client = server->nextPendingConnection();
        

        Hello memleak...

        Any How when unappropriate ip adress try to connect my app crash

        Then use a debugger to find out where it crashes, your code above does not create a crash, just a memleak as shown above.

        gfxxG Offline
        gfxxG Offline
        gfxx
        wrote on last edited by gfxx
        #3

        @Christian-Ehrlicher ok understand .... but my code is ok for close connection when non desired IP adress try to connect? These is a right manner?

        Need di dischard/disconnect every undesired ip adress ....

         /*peerF, peerBR, peerB was QString with ip adress needed*/
          QString conClient = client->peerAddress().toString();
        
            if((conClient == peerF) || (conClient == peerBR) || (conClient == peerB)){
                  /* .... other code. ... */
            }
            else{client->deleteLater(); qDebug() << "discharge socket";}
        

        About memory leak :

        /*client = new QTcpSocket();*/
        
        client = server->nextPendingConnection();
        

        these can be?

        bkt

        Christian EhrlicherC 1 Reply Last reply
        0
        • gfxxG gfxx

          @Christian-Ehrlicher ok understand .... but my code is ok for close connection when non desired IP adress try to connect? These is a right manner?

          Need di dischard/disconnect every undesired ip adress ....

           /*peerF, peerBR, peerB was QString with ip adress needed*/
            QString conClient = client->peerAddress().toString();
          
              if((conClient == peerF) || (conClient == peerBR) || (conClient == peerB)){
                    /* .... other code. ... */
              }
              else{client->deleteLater(); qDebug() << "discharge socket";}
          

          About memory leak :

          /*client = new QTcpSocket();*/
          
          client = server->nextPendingConnection();
          

          these can be?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @gfxx said in How block/cancel specific IP adress:

          but my code is ok for close connection when non desired IP adress try to connect?

          Yes

          these can be?

          correct

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          gfxxG 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @gfxx said in How block/cancel specific IP adress:

            but my code is ok for close connection when non desired IP adress try to connect?

            Yes

            these can be?

            correct

            gfxxG Offline
            gfxxG Offline
            gfxx
            wrote on last edited by
            #5

            @Christian-Ehrlicher Real Thanks ... and happy sunday.

            bkt

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved