Qt World Summit: Submit your Presentation

Connect to non-local IP or domain name

  • I have 2 question
    1-if I want to connect to non-local IP with QTcp Socket ,what should I do ?

    QHostAddress address ("");

    I use this to connect to the local IP ,but what to use if non-local IP?
    If IP is not within my IP range,what should we do?
    for example my IP is
    and the server IP is
    f I have domain name can use this?

    QHostAddress address ("mydomainName.com");

    2-If I want to connect to sqlserver that it in non-local ,what should I do ?

    db = QSqlDatabase::addDatabase("QODBC");
        bool test=db.isValid();//true
        db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost\\SQLSERVER;DATABASE=DB1");

    I use this to connect to a local database, but what should I do if the IP is non-local?
    What if the IP is not within my IP range?
    What should I do?
    What if the database address is a domain name?

  • Lifetime Qt Champion

    To connect a server, the firewalls need to allow it.
    So the routers and switches will route your incoming connections to the right pc.
    Even if you are not on same subnet.
    So it will just work if the infrastructure allows it.

    A domain name is merely a human readable version of an ip.
    so makes no difference.
    you always really connect to an IP and a port.
    often the port is determined by the protocol. Like 80 for http etc.

    You will connect to an IP and port and if database server is behind a firewall,
    it must allow that traffic.

  • @mrjj So this code works without modification, If ip remotely allows me to connect?
    I do not need to change the code or make a custom setting on the client device?

  • @isan

    The network modules works with intra and extra network acess. But the responsable to forward your connection if necessary is the gateway of your network.

    A little resume of how network communication works:

    If the IP A and B are in the same network, A will send directly the packet to B because they are in the same network.
    But if the IP A and B are in different network, A will send the packet to default gateway of network and the gateway will be the responsable to find the next route to send the packet to B and so on until the packet reaches the destination or be discarded.

    But the communation can be refused for n reasons:

    • The gateway don't know how forward the connection (next route)
    • The gateway can refuse connection for a specific ip of network
    • The firewall can deny the connetion, etc.

    So, notice that it is a infraestruture problem and not a software problem.

  • Lifetime Qt Champion

    On top of @KillerSmath good explanation, i can highly recommend to read some
    networking to have an overview of how it works.

  • @mrjj so whats difference between network and websockets module?
    for connect to web server or non-local IP should use witch one?

  • Lifetime Qt Champion

    Websockets is a protocol designed for 2 ways interaction with
    web servers.
    Where as the network classes are more generic/broad to talk to any kind of
    IP device.

    To talk to an non local database server, you would still use the SQL classes.
    It really dont matter if local or on global scrope. Still works the same except
    there will be firewall in front if outside.
    Often if you buy the database access as service, its already set up to allow connections.
    If you own server. you need to make it possible.

  • @mrjj So If I want to connect to web server , can I use network module?

  • @isan
    If you are trying to connect to SqlServer database using tcp ip, you can find an explanation in the ODBC Qt Wiki:
    Open Connection using TCP/IP

    Remember: The firewall of your server needs to allow the connection by port or service or it will deny the connection.

  • Lifetime Qt Champion

    @isan Above you wanted to connect to a SQL server, now to a web server. So, what do you really want to do?

  • @jsulm I Had two different question on first ,non-local IP can be anything

  • Lifetime Qt Champion

    Yes you can use the network module to talk to web server.
    Here is sample that downloads a file

    so rest depends on WHAT you want to do with the web server.

  • @mrjj thank you so much

Log in to reply