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. check port in use
Forum Updated to NodeBB v4.3 + New Features

check port in use

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 3.0k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    s002wjh
    wrote on last edited by
    #1

    whats best/easy way to check if port are already in use/listen?

    W 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAIK, one simple way is to try to connect to that port but you can have a race condition as between the moment you check and the moment you continue your logic, another process may have requested said port.

      What is your use case ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S s002wjh

        whats best/easy way to check if port are already in use/listen?

        W Offline
        W Offline
        wrosecrans
        wrote on last edited by
        #3

        @s002wjh Just try to listen on it. If you can listen on it, then it wasn't already in use. If you can't listen on it, it might already be in use.

        S 1 Reply Last reply
        2
        • W wrosecrans

          @s002wjh Just try to listen on it. If you can listen on it, then it wasn't already in use. If you can't listen on it, it might already be in use.

          S Offline
          S Offline
          s002wjh
          wrote on last edited by s002wjh
          #4

          @wrosecrans thx, is there example or syntax on listen the port? using http qtwebapp, I have a browser that use local port for the server, and want to make sure there is no conflict with local host port.

          JonBJ 1 Reply Last reply
          0
          • S s002wjh

            @wrosecrans thx, is there example or syntax on listen the port? using http qtwebapp, I have a browser that use local port for the server, and want to make sure there is no conflict with local host port.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @s002wjh
            In Qt you have https://doc.qt.io/qt-5/qtcpserver.html#listen.
            Linux has http://man7.org/linux/man-pages/man2/listen.2.html
            Windows has https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen

            1 Reply Last reply
            3
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @s002wjh said in check port in use:

              is there example or syntax on listen the port

              See QTcpServer::listen()

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

              1 Reply Last reply
              3
              • S Offline
                S Offline
                s002wjh
                wrote on last edited by
                #7

                can I do this without creating new tcpserver etc?

                int test;
                test=QTcpServer::listen(QHostAddress::Any, 1234);

                so if there is something using port 1234 it return 0 or false?

                jsulmJ 1 Reply Last reply
                0
                • S s002wjh

                  can I do this without creating new tcpserver etc?

                  int test;
                  test=QTcpServer::listen(QHostAddress::Any, 1234);

                  so if there is something using port 1234 it return 0 or false?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @s002wjh said in check port in use:

                  can I do this without creating new tcpserver etc?

                  No, because listen() is not static

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  4

                  • Login

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