How to get the address of server dynamically?
Let suppose I have a server installed on computer A. It's listening through QHostAddress::Any on port 8888.
I have provided a client copy to users. On it's login page I want them to get connected to server which is connected through wifi. so can anyone please tell me how to get that IP address of Server. Is there any function or anything that I can use.
Did you try "serverAddress":http://qt-project.org/doc/qt-4.8/qtcpserver.html#serverAddress ?
I want my client to know about that :)
You will have to use a service discovery protocol like SSDP or Zeroconf (aka. Bonjour or Avahi). There are no implementations in Qt for this out-of-the-box, but you can use for example the "QxtZeroconf module":http://libqxt.bitbucket.org/doc/0.6/qxtzeroconf.html.
If you're connecting via a host name, you can find out the IP address after connecting using http://doc-snapshot.qt-project.org/5.0/qabstractsocket.html#peerAddress
If you are talking about discovering a server on a LAN, do what Lukas said or use UDP multicast/broadcast.
If you are talking about discovering a server on the internet, then you need a master server with a DNS name somewhere.
Thanks for the reply guys! I am trying to do this :)
- On your client side create a QTcpSocket and a QUdpSocket and on the server side create a QTcpServer and a QUdpSocket.
- On the client, send out a broadcast message saying "who is out there, i am a client"
- Once the packet is received on the server, send a direct UDP packet back to the QUdpSocket saying "i am here, pick me this is my tcp ip address and tcp port" (you can get the address and port from the received packet). I usually populate a QListView with the ipaddress and ports so that user can choose who he wants to connect to.
- When your ready to connect, just take the tcp address and port sent in the packet and connect the QTcpSocket to the QTcpServer.
It's basically your standard SSDP as was stated above.
Thanks! It worked! :)
yes your idea working..