Solved 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 ("172.20.10.151"); _socket->connectToHost(address,8585);
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 172.20.10.100
and the server IP is 192.168.1.240
f I have domain name can use this?QHostAddress address ("mydomainName.com"); _socket->connectToHost(address,port);
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 test=db.isDriverAvailable("QODBC");//true db.setHostName("localhost\\SQLSERVER"); 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? -
Hi
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? -
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.
-
On top of @KillerSmath good explanation, i can highly recommend to read some
networking to have an overview of how it works.
http://www.steves-internet-guide.com/basic-networking-course/ -
@mrjj so whats difference between network and websockets module?
for connect to web server or non-local IP should use witch one? -
Hi
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/IPRemember: The firewall of your server needs to allow the connection by port or service or it will deny the connection.
-
@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
-
@isan
Hi
Yes you can use the network module to talk to web server.
Here is sample that downloads a file
http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.phpso rest depends on WHAT you want to do with the web server.
-
@mrjj thank you so much