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. Fortune examples (client and server)
Qt 6.11 is out! See what's new in the release blog

Fortune examples (client and server)

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 4.2k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    In a DHCP environment, you would use the (host)name of the server and the DHCP server would resolve the IP. If you know the name of the server, try ping it from the connecting pc to verify it can find it.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Diarby
      wrote on last edited by
      #3

      Hi,
      Thanks for your reply. I can easily retrieve the IP address of the server. When I ping it, I receive an echo but nothing about the port number.
      Is there another way to find it ?

      mrjjM 1 Reply Last reply
      0
      • D Diarby

        Hi,
        I'm reading these examples to learn how to connect a client (on one machine) to a server (on another machine) using QTcpSocket. All examples found use the same machine for client and server.
        Before the client can connect [ function connectToHost() ] to a server, it needs the server IP (or hostname) and port. The IP can be found whith a call to QHostInfo::lookupHost(). On my machine, I can read the port where server is listening to.
        But if I was running client from a remote machine, how can I find the server' port (as this one is randomly supplied on server start) ?
        From QTcpSocket, I read peerPort() "returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0". This state is achieved after connection, but before to connect I need this port. I'm blocked there and need help.

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #4

        @Diarby said in Fortune examples (client and server):

        as this one is randomly supplied on server start

        Do you have no way of fixing it? if you are creating the server yourself, I don't see why you shouldn't be able to fix the port

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • D Diarby

          Hi,
          Thanks for your reply. I can easily retrieve the IP address of the server. When I ping it, I receive an echo but nothing about the port number.
          Is there another way to find it ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Diarby
          Hi normally the port is NOT random, only ip. ?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Diarby
            wrote on last edited by
            #6

            Hi,
            I'm using the Fortune Server and his client from Qt. As I have server and client, I know all information about server.
            My problem is the client will be run by anyone somewhere in the world. And the server in another machine (not mine) with a name example.com. When the client is run, it must connect to that machine and I want to configure connection by code as the client user have no idea about server. As Fortune uses QTcpSocket, the client must provide IP and port. The IP is resolved with QHostInfo. It remains to resolve the port number. From the Qt example, the server produces this value with a call to serverPort() not known before running the server.

            mrjjM 1 Reply Last reply
            0
            • D Diarby

              Hi,
              I'm using the Fortune Server and his client from Qt. As I have server and client, I know all information about server.
              My problem is the client will be run by anyone somewhere in the world. And the server in another machine (not mine) with a name example.com. When the client is run, it must connect to that machine and I want to configure connection by code as the client user have no idea about server. As Fortune uses QTcpSocket, the client must provide IP and port. The IP is resolved with QHostInfo. It remains to resolve the port number. From the Qt example, the server produces this value with a call to serverPort() not known before running the server.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #7

              @Diarby
              Hi the server could be listing on any port. So there is no way to know which port would be right. That is normally fixed and not varying.
              Also if it runs outside in he world, it will behind a firewall and that firewall must allow the port.

              D 1 Reply Last reply
              0
              • mrjjM mrjj

                @Diarby
                Hi the server could be listing on any port. So there is no way to know which port would be right. That is normally fixed and not varying.
                Also if it runs outside in he world, it will behind a firewall and that firewall must allow the port.

                D Offline
                D Offline
                Diarby
                wrote on last edited by
                #8

                @mrjj Hi. Can I use a value of 0 ?

                mrjjM 1 Reply Last reply
                0
                • D Diarby

                  @mrjj Hi. Can I use a value of 0 ?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Diarby
                  Hi
                  That is not a good port.
                  You could use some random one for the server app and clients would always try that.
                  But you might be blocked by firewalls.

                  D 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Hi
                    https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
                    Ports are normally used pr service the server provides.
                    You are free to use one not taken.
                    BUT can u trust the users to open ports?

                    D 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @Diarby
                      Hi
                      That is not a good port.
                      You could use some random one for the server app and clients would always try that.
                      But you might be blocked by firewalls.

                      D Offline
                      D Offline
                      Diarby
                      wrote on last edited by
                      #11

                      @mrjj
                      Hi. I'd try to scan all ports (from 1 to 65535) until I found the exact one. For each, I'll try a connection.
                      Is this solution suitable ?

                      mrjjM 1 Reply Last reply
                      0
                      • D Diarby

                        @mrjj
                        Hi. I'd try to scan all ports (from 1 to 65535) until I found the exact one. For each, I'll try a connection.
                        Is this solution suitable ?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Diarby
                        well if user run some local webserver or other service it might not work but
                        yes it could work.

                        You do understand that most home routers comes with most ports disabled ?
                        and wont allow access unless open in router.

                        So if you deployment involves random user around the world and u try to connect to them from outside, you
                        might get issues.

                        1 Reply Last reply
                        2
                        • mrjjM mrjj

                          Hi
                          https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
                          Ports are normally used pr service the server provides.
                          You are free to use one not taken.
                          BUT can u trust the users to open ports?

                          D Offline
                          D Offline
                          Diarby
                          wrote on last edited by
                          #13

                          @mrjj
                          Thanks for this link. I was writing when you sent it. Please give me time to read it and I'll be back tomorrow

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            Hi normally the users are running the client that connects to a known IP+port to talk
                            to server. It sounds like you plan to do the reverse?

                            D 1 Reply Last reply
                            1
                            • mrjjM mrjj

                              Hi normally the users are running the client that connects to a known IP+port to talk
                              to server. It sounds like you plan to do the reverse?

                              D Offline
                              D Offline
                              Diarby
                              wrote on last edited by
                              #15

                              @mrjj
                              Hi
                              I just finished reading the wikipedia article. From your last post, I agree to your opinion : IP + port is known by clients.
                              I was disturbed by Fortune Server which changes port on every launch.
                              A subsidiary question, why Qt let newbies learn client/server programming with this example ?

                              VRoninV mrjjM 2 Replies Last reply
                              0
                              • D Diarby

                                @mrjj
                                Hi
                                I just finished reading the wikipedia article. From your last post, I agree to your opinion : IP + port is known by clients.
                                I was disturbed by Fortune Server which changes port on every launch.
                                A subsidiary question, why Qt let newbies learn client/server programming with this example ?

                                VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by VRonin
                                #16

                                @Diarby said in Fortune examples (client and server):

                                I was disturbed by Fortune Server which changes port on every launch.

                                Just change if (!tcpServer->listen()) to if (!tcpServer->listen(QHostAddress::Any,13008)) in the server then you can be 100% sure the port will be 13008

                                A subsidiary question, why Qt let newbies learn client/server programming with this example ?

                                I know, it's proper garbage.
                                Try page 323 (341 of the PDF) of C++ GUI Programming with Qt4. It's a bit old and QDataStream evolved and made everything easier but it's still better than the Fortune example

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                1 Reply Last reply
                                2
                                • D Diarby

                                  @mrjj
                                  Hi
                                  I just finished reading the wikipedia article. From your last post, I agree to your opinion : IP + port is known by clients.
                                  I was disturbed by Fortune Server which changes port on every launch.
                                  A subsidiary question, why Qt let newbies learn client/server programming with this example ?

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @Diarby
                                  Ok, so it will not be reversed in your case?
                                  The Fortune examples just teach how to use the Qt classes.
                                  Its not really up to the task of teaching basic networking. ( or not included at least)
                                  But there are tons of info about that on net.

                                  D 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @Diarby
                                    Ok, so it will not be reversed in your case?
                                    The Fortune examples just teach how to use the Qt classes.
                                    Its not really up to the task of teaching basic networking. ( or not included at least)
                                    But there are tons of info about that on net.

                                    D Offline
                                    D Offline
                                    Diarby
                                    wrote on last edited by
                                    #18

                                    @mrjj
                                    Hi, Thanks a lot for your help. I learn more today about programming with Qt.
                                    @VRonin
                                    Thanks for tips.
                                    For all of you, God blesses you.

                                    1 Reply Last reply
                                    1

                                    • Login

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