I have made an android and desktop application in which my phone and laptop are on the same wifi and I am able to exchange data between them.
My desktop application has a logic of QTcpServer while the android application is based on QTcpSocket. Everything is working fine.
How do I make this work when the phone and laptop are on separate networks? Like my phone is on 3g and laptop on Wifi. Can this work somehow?
In your test when both are on the same wifi you could find out the ip address of your phone or laptop running the server, correct?
You required the ip address for tcp socket to establish the connection.
When can setup your server application somewhere with a static ip address, it does not really matter which type of communication means (wifi, cable, 3g or whatever) you have.
The problem is in most cases that you have only dynamic ip addresses. HOwever, there are possibilities like noip. Unfortuntely, I have no experience with such a service.
My server is on listening mode on a specific port no and it's address is set to Any. My phone just connects to the IP address of the PC and the specified port no and I'm good to go.
But I'm not able to understand how to take this further when both are not on the same network. I also read something related to static IP setup somewhere but I have no clue whatsoever.
As koahnig already mentioned if you have a dynamic dial up connection get yourself a noip/dyndns/... service, which will map a virtual domain name (most of the time a subdomain, some also provide the possibility to use your own domain) to your current ip. Therefor you will have to run a client within your network which updates the serviceprovider mapping to your external ip. This client is provided by the service maintainer and there are also some free clients available (e.g. ez-ipupdate).
The next step is to statically forward (or use upnp forwarding when implemented) your required ports from your external ip to your internal server (alternativly setup your internal server as DMZ, but this will put your server into the public).
As the last step just configure your mobile application to not directly connect to your server or searching for your server, but have it connect to your virtual hostname with the required port.
Both of the above answers are spot on.
I think it is important you fully understand LAN versus WAN or local versus wide when it comes to networks.
We don't have enough info to help you debug this. We'd need to know exactly where your server is located (LAN, etc) and if so have you exposed the IP and port through any routers or firewalls?
For example in my office I have a cable modem. This cable modem gives all of my local devices including those on WiFi a local IP address, something like 10.1.10.XXX.
on the WAN my IP is something MUCH different and will depend upon your network provider. The general process for communicating between a device on the WAN to something on your LAN looks generally like this:
That process so far gets you to the wire leading into your router whatever it may be. If it has a built in firewall you have to make sure that whatever port you are using is routed to your local device.
Anyway somehow your request has to get from your public IP of 22.214.171.124 to your LAN IP which might be something like 10.1.10.111. This is generally handled by routing or firewall rules in your router.
Another way to handle this is as one other replier stated above: Using some service like dyndns or other you can publish a unique name that will correctly route your external requests to your internal server. But again if you have a firewall or router you'll still need to allow that traffic.
Hope this helps. My point of typing this all in is that simply put the networking in Qt can work across anything. As mentioned above it doesn't matter if it is WiFi, Edge, 3G, LTE or whatever. But getting your requests and packets routed correctly is not trivial.
You'll need a third known server to which you can both announce the IP address before you can start a peer-to-peer connection.
First, you need to have public IP on your server PC, that is:
a) you must have only one router (not two or more cascaded in your home), and
b) you must use UPnP to open the ports you want to listen on. Unfortunately, for whatever reason, Qt never bothered to at least port a UPnP library (e.g. miniupnp) to Qt.
Second, you need to know, from the outside, sour server's IP: for this you need a "known server" with a known static IP where you should publish your server PC's IP each time the server starts, or use something like dyndns.
Finally, even with the setup above, you can only use this solution if your server PC does not connect via a mobile network, because in that case you can't open any UPnP ports on your provider. (see http://stackoverflow.com/questions/13996361/getting-around-nat-for-p2p-app-if-i-have-all-ip-info/14202854#14202854 )