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. [solved] tpc socket binding problem
Forum Updated to NodeBB v4.3 + New Features

[solved] tpc socket binding problem

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 7.5k Views 2 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.
  • G Offline
    G Offline
    gyll
    wrote on last edited by gyll
    #1

    I have a simple console app which i wrote to test qt's tcp sockets, as follows:

    When the app starts is asks me (console input) if i want it to be a client or a server; then:

    1. if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
    2. if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)

    Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?

    K G 3 Replies Last reply
    0
    • N Offline
      N Offline
      Nando
      wrote on last edited by Nando
      #2

      Hi,
      for a client yo do not need to bind to a port.
      Just call

      tcpSocket->connectToHost(hostname, port);

      see the example: link text

      If you explicitly want to use a specic port on client side it must differ from the server port if both processes run on the same machine.

      If you still get this error check for example if you have another service running on port 8080 (maybe a proxy?)

      Greetings
      Nando

      G 1 Reply Last reply
      0
      • G gyll

        I have a simple console app which i wrote to test qt's tcp sockets, as follows:

        When the app starts is asks me (console input) if i want it to be a client or a server; then:

        1. if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
        2. if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)

        Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @gyll
        You would need to listen with QTcpServer on a specific port (e.g. 8080).
        The same port would be used with QTcpSocket to connect.

        There are examples available Fortune server and Fortune client. This will help you in understanding.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • N Nando

          Hi,
          for a client yo do not need to bind to a port.
          Just call

          tcpSocket->connectToHost(hostname, port);

          see the example: link text

          If you explicitly want to use a specic port on client side it must differ from the server port if both processes run on the same machine.

          If you still get this error check for example if you have another service running on port 8080 (maybe a proxy?)

          Greetings
          Nando

          G Offline
          G Offline
          gyll
          wrote on last edited by
          #4

          @Nando i don't get it, maybe you didn't read my post carefully? the client and server sockets are on DIFFERENT ports (server on 8080, client on 8081, i even marked that in bold in my post)

          and no, there's no other service on port 8080, if i start the server first than it binds properly.

          N 1 Reply Last reply
          0
          • G gyll

            @Nando i don't get it, maybe you didn't read my post carefully? the client and server sockets are on DIFFERENT ports (server on 8080, client on 8081, i even marked that in bold in my post)

            and no, there's no other service on port 8080, if i start the server first than it binds properly.

            N Offline
            N Offline
            Nando
            wrote on last edited by
            #5

            @gyll Please show us your code.
            I read it. And see that you use different ports, but somehow either your have a service already running on this port or maybe you have a code issue. (same port variable from config for both modes client / server?)

            Did you remove the bind from the client code?
            You do not need this.

            Greetings
            Nando

            G 1 Reply Last reply
            0
            • N Nando

              @gyll Please show us your code.
              I read it. And see that you use different ports, but somehow either your have a service already running on this port or maybe you have a code issue. (same port variable from config for both modes client / server?)

              Did you remove the bind from the client code?
              You do not need this.

              Greetings
              Nando

              G Offline
              G Offline
              gyll
              wrote on last edited by gyll
              #6

              @Nando

              1. i need to specify the client-side port, please don't tell me what i need and what i don't
              2. i tried to remove the binding of the client (i.e. allow it to chose its own port) but the same happens: the server's 'listen()' returns "The bound address is already in use" error
              3. if i start only the server (i.e. the client is not stated at all) then the server binding (i.e. 'listen()') apparently works (or, at the very least, it does not return any error)
              4. i cannot paste the code here, it's way too big. i'm writing my own wrapper classes over Qt's sockets and they are way to big to list here
              1 Reply Last reply
              0
              • G gyll

                I have a simple console app which i wrote to test qt's tcp sockets, as follows:

                When the app starts is asks me (console input) if i want it to be a client or a server; then:

                1. if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
                2. if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)

                Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                You are right that I did not read care full enough. Sorry about that. My understanding was that your problem is more basic.

                @gyll said:

                I have a simple console app which i wrote to test qt's tcp sockets, as follows:

                When the app starts is asks me (console input) if i want it to be a client or a server; then:

                1. if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
                2. if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)

                Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?

                Basically there should be no problem in what you are trying to do. You can use a couple of QTcpClients and a couple of QTcpServers in parallel on the same machine as well as in the same application at the same time. I'm doing this all the time.

                You may want to check with this or similar statement:

                        Server->listen ( QHostAddress :: Any, 8080 );
                

                Possibly your port is already occupied by a completely different application or by a copy of your application (left over from trials).
                Therefore, you need to modify your code for checking if it is an assigned port problem.

                Next step would be using another port number just to see, what the error message is there.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • G gyll

                  I have a simple console app which i wrote to test qt's tcp sockets, as follows:

                  When the app starts is asks me (console input) if i want it to be a client or a server; then:

                  1. if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
                  2. if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)

                  Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?

                  G Offline
                  G Offline
                  gyll
                  wrote on last edited by
                  #8

                  okay, my mistake, there's no problem with the sockets. instead of instantiating only the server in the server app and the client in the client app, i was indeed instantiating both sockets twice (once in the client app and then again in the server app)

                  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