Results 1 to 8 of 8

Thread: How to get the address of server dynamically?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get the address of server dynamically?

    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.

  3. #3
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get the address of server dynamically?

    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!

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get the address of server dynamically?

    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.
    Qt Code:
    1. for( int i = 0; i < 256; ++i )
    2. {
    3. QHostAddress ha( QString( "192.168.10.%1" ).arg( i ) );
    4. // connect to this address and see what happens.
    5. }
    To copy to clipboard, switch view to plain text mode 
    Only problem is that it may take long and you may trigger some firewalls on target machines.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get the address of server dynamically?

    You might be able to use ZeroConf for service discovery.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get the address of server dynamically?

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get the address of server dynamically?

    Zeroconf does a udp multicast. The advantage is all firewalls should let it through by default since it acts as DNS traffic.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get the address of server dynamically?

    Great! I will try this. However, the firewall problem is significant!

Similar Threads

  1. [TCP Server] Server only sending when terminating
    By papperlapapp in forum Qt Programming
    Replies: 0
    Last Post: 6th December 2010, 19:41
  2. getting IP-address
    By jefklak in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2010, 09:19
  3. Replies: 2
    Last Post: 28th January 2010, 06:26
  4. how can i get IP address
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2009, 05:27
  5. IP Address
    By rajveer in forum Qt Programming
    Replies: 2
    Last Post: 11th November 2008, 08:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.