Important: Please read the Qt Code of Conduct -

Principles in connecting multiple apps in an AD-HOC connection

  • Greetings!

    I want to start coding a small game that can be played in an ad-hoc (wifi) connection.

    Although I knew and made apps about QTcpSocket, QTcpServer, QUdp.... etc. I don't find any examples about the basic principles when it comes to ad-hoc connection. I don't know what is its difference to "direct connection" since it works in wireless.

    Anyone wants to explain me or give me a link (tutorial/examples/etc) on how apps can be connected within an ad-hoc connection.

    Thanks a lot and more powers

  • Most types of (wireless) network are centralized. Those are not "ad hoc" at all. For example, in WLAN's there is the WLAN access-point somewhere to which all clients connect. Data is always sent to the access-point and from there it is forwarded to the destination. The data is never sent directly from client to client. Because the access-point has connection to all clients, routing is very easy. At the same time, in an "ad hoc" network, there is no "central" unit which could coordinate the transfer! Data needs to be sent from client to client directly. But how to know which clients are reachable? Nearby clients can be found via broadcast, okay. But what, for example, if client A can reach client B and client B can reach client C, but A cannot reach C directly? Now we need to send the data from A to B and then B must forward it to C (or the other way around). This needs rather complex routing protocols! Especially because in an "ad hoc" network existing clients may disappear at any time, while new clients will appear now and then. Also clients may stay in the network, but move around to another location (if A could reach B directly 5 minutes ago, this may no longer be possible now). This means routing needs to be highly dynamic in "ad hoc" networks! Finally there may be "malicious" clients which pretend they can reach everyone, although in fact they just discard all data they get. It needs rather complex reputation-based algorithms to find a "reliable" route in the presence of "malicious" clients.

    But all this is stuff that happens on the lowest layers, like layer 2 ("Data Link") and layer 3 ("Network"). TCP or UDP connections are on layer 4 ("Transport") and usually don't have to care about the realization of the lower layers. For example, if you create a Game that connects players via TCP, you would just open a TCP connection and that's it. Whether (on the lower layers) the data gets transferred via Ethernet (wired LAN) or via WLAN or maybe via some kind of "ad hoc" network shouldn't matter - unless you are driver or OS developer...

    See also:

  • Thanks MuldeR! I will be reading that link.

    I am just curious that if I connect using QTcp/QUdp, what would be the Host address and/or port address "common" or "accessible" to both PCs? assuming that there are only 2 computers connected and one of which provides the server for the ad hoc.

    Can I find this with a command like "ipconfig in windows? though I am not sure about this.

    Thanks and more powers!

  • Well, TCP or UDP connections are always established between Sockets (end-points). And a Socket is usually defined by the IP-Adress plus the Port-Number. But if we are talking about IP-Adresses and Port-Numbers, we are already making the assumption that we are building on top of the Internet Protocol (IP). Whether and how an "ad hoc" network provides an IP-Stack probably depends on the individual implementation. What kind of "ad hoc" network are you using exactly? Or do you just ask about "ad hoc" networks in general?

    Now if we assume that we do have an IP-Stack available, each computer is identified by its IP-Address. Normally IP-Addresses are either configured statically or (more commonly these days) assigned via DHCP. In the latter case the DHCP server assigns each host a valid IP-Adress. Starting with IPv6, there also is a new method how a host can auto-generate a valid (link-local) IP-Address for itself. The last one is probably the method you'd need for an "ad hoc" network, because in an "ad hoc" network there won't be a "central" unit, like a DHCP server.

    Big question: How do we get the IP-Address of the "other" computer? As long as the other computer is located within the same local network, we could do some kind of broadcast/flooding. It's like we send a packet with the content "if you (the target computer) get this packet, please reply and tell me your address" to everybody on the network. Then we wait for a reply! Because broadcasts are usually not routed to outside the local network (for good reason ^^), other methods are needed if the "other" computer may be located in a different network - which in the Internet almost always is the case. In that case we usually have a server which is reachable on the Internet under a fixed and well-known IP-Address (or DNS-Name). Then both computers will connect to that server under its known address. The server will then negotiate the connection between the client computers...

  • Moderators

    To discover IP addresses to talk to you could also look into bonjour/rendezvous. It is a way to announce and discover services on a local network.

  • Thank MulDer and Tobias.

    I tried to google for resources and found these links:

    It tells something like then I checked my WLAN and found

    Though haven't tested yet. It seems feasible! I will be testing these in no time.

    Thanks and more powers!

  • In IPv4 there are (at least) two address ranges reserved for local networks. Those are:

    192.168.x.y and 10.x.y.z

    Those addresses are only valid/visible within the local network, but they are not accessible from another network. Most important, a computer that is supposed to be reachable from the Internet needs a "public" (non-local) IP address. Usually only the router gets a "public" IP address, from your ISP. But routers can be configured to translate between "public" and "private" (local) IP addresses, allowing local computers to communicate with computers outside the local network. It's called "network address translation":

    However that all doesn't help you much. If the "other" computer is within the same local network as you, it most likely is reachable under some 192.168.x.y or 10.x.y.z address. But you don't know under which one. And if the "other" computer is in a different network, it isn't reachable under it's "local" IP address anyway.

    Ergo: You still need a broadcast, a server that has a well-known address or some kind of announcement service...

    (Also we are now far away from the original discussion about "ad hoc" networks)

  • Thanks Mulder!

Log in to reply