PDA

View Full Version : How to get the address of server dynamically?



codeartist
27th May 2012, 15:40
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.

Cruz
28th May 2012, 00:09
Does the server have a static IP address? In that case you could just hardcode the address of the server. Or is there a name resolution in place so that you can use a hostname to connect instead of an IP address? Otherwise you can periodically broadcast or multicast "hello" packets from the server that tells potential clients on the network where to find said server. Or you can have each client scan a subnet and see if it gets a response from a server on any IP address.

codeartist
28th May 2012, 08:37
No the server don't have a static IP address. and It's a college computer so can't have a hostname. Can you give me some code about what you said

Or you can have each client scan a subnet and see if it gets a response from a server on any IP address.

It will be a great help to me!

Spitfire
30th May 2012, 09:44
If you can (ie if your network allows) broadcast/multicast is the way to go.
Unfortunatley it may not work on some networks as routers usualy block broadcasts.

As to the other suggestion, it's fairly simple. just get the client to attempt to connect to all 255 ips on the sub-net and see if any is successful.


for( int i = 0; i < 256; ++i )
{
QHostAddress ha( QString( "192.168.10.%1" ).arg( i ) );
// connect to this address and see what happens.
}

Only problem is that it may take long and you may trigger some firewalls on target machines.

wysota
31st May 2012, 00:22
You might be able to use ZeroConf for service discovery.

StrikeByte
31st May 2012, 09:32
Hi,

It is also possible to do an udp broadcast from the clients to discover the server
What you have to do is let the client broadcast a specific udp package when it has no connection, when the server receives it, it replies and then you know who the server is.

wysota
31st May 2012, 09:46
Zeroconf does a udp multicast. The advantage is all firewalls should let it through by default since it acts as DNS traffic.

codeartist
1st June 2012, 04:58
Great! I will try this. However, the firewall problem is significant!