Fortune examples (client and server)



  • 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.


  • Qt Champions 2017

    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.



  • 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 ?



  • @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


  • Qt Champions 2017

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



  • 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.


  • Qt Champions 2017

    @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.



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


  • Qt Champions 2017

    @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.


  • Qt Champions 2017

    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?



  • @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 ?


  • Qt Champions 2017

    @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.



  • @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


  • Qt Champions 2017

    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?



  • @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 ?



  • @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


  • Qt Champions 2017

    @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.



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.