0

I have written a game that users will download. When the game is started, I want it to automatically connect to the matchmaking server. Does that mean that, somewhere in the source code for the game, I should have something like

url = "www.mygame.com/whatever"
port = somenumber

Are there other options to connect to a server? Do all programs that connect to an external server (i.e., outside of the local network) over the internet use a hard-coded url and port number?

9
  • Genuine question so people can write answers which may be helpful: do you understand the difference between TCP/UDP and HTTP? Commented May 8 at 8:11
  • @PhilipKendall I don't know much about HTTP, but I understand how it is built on TCP/UDP. I studied networking from Tanenbaum a few years ago, but I wrote my own application protocols in boost::asio that always connected back to my own computer. If the answer to the OP is in the affirmative, then I am pretty confident I know enough to write the networking code I need to write. I want to confirm if the way I plan to write my new code is a way of doing it or the only way of doing it as part of a sanity check for my memory.
    – Bobby
    Commented May 8 at 8:33
  • 1
    Hint: "URL" is not a concept which exists in either TCP or UDP. Are there ways to connect to things which don't involve a hard-coded URL? Commented May 8 at 9:01
  • 2
    Let's think about it - you say app needs to connect to a server. Well, it needs to know at least the server address somehow (be it URL or IP). Port and path (and arguments) could probably be hardcoded.
    – Kromster
    Commented May 8 at 9:13
  • @PhilipKendall an IP address? I guess I assumed getting a static IP address was very hard. That answers one question: whether or not there are other options. I guess the second question is the main OP, which is preferable for this example: a hard-coded URL or a hard-coded IP address?
    – Bobby
    Commented May 8 at 9:19

2 Answers 2

3

Either a stable IP assignment or a DNS registration is going to require annual payment of some kind. The use of a DNS name will nearly always serve you better, and is easier to buy and reserve, so that should be your default choice. Cautious people go one step further and specify a list of servers to be tried in order, which would let you have both.

Make sure you're pointing to a sub-record such as "matchmaking.examplegame.com". This is better than "www.examplegame.com" or bare "examplegame.com" because you may want to point them at different systems.

I would also suggest putting it somewhere in the game assets so it's easier to change if you ever need to do so.

For the long term, you should include IPv6 support in case it ever needs to work somewhere that IPv4 is deprecated. A long way away, but IPv4 addresses are gradually getting more expensive.

1

Should non-browser applications connect to a URL?

Not necessarily 'should' but they can and do connect to URLs. There's no reason to think they should not use URLs.

It's not really relevant to your question but URLs can be used for many things aside from websites. You can reference a local file with a URL/URI, for example. Again, not necessarily relevant here, just background knowledge.

DNS is essentially a like a directory for ip-addresses. Ultimately, your application will connect to an IP address. DNS is essentially a way to find the ip-address assigned to a name. This means your IP address can move around. For example, if you map to a specific IP address and then later decide you hate your cloud provider or hosting service, you are SOL. IP addresses are not portable. A DNS name, however, would be owned by you and can be repointed to other hosting solutions.

hard-coded url and port number

It doesn't hurt to allow for a port number but I'm not sure you really need that. If you use HTTPS, the port number is assumed to be 443 unless you specify otherwise. And HTTPS is probably what you want. DNS has been improving over time to be more secure, but it's a bad idea to fully trust DNS name resolution. Using HTTPS will allow for your client to verify that the host it is connecting to is a host that you control. Even if you are not concerned with encrypting the traffic, host verification is crucial for securing an internet-based solution.

7
  • Not all urls are http. smtp://[email protected] ftp://example.com:file and ssh://[email protected] are all valid urls. Commented May 8 at 19:28
  • 1
    @user1937198 Since I mention that fact in my answer, I'm not sure what your intention is with your comment.
    – JimmyJames
    Commented May 8 at 19:32
  • @JimmyJames One reason I had for specifying a port number is that I may want several different applications (all with minuscule user-bases) to use the same server to save some money.
    – Bobby
    Commented May 8 at 22:31
  • @Bobby That's a good reason and good forward planning; and if you give them different DNS names, you have the opportunity to move them off individually to different servers in the future if you need to.
    – pjc50
    Commented May 9 at 10:43
  • @Bobby You have separate DNS names as pjc50 mentions or just create different subdomains. Are you thinking of doing something other than HTTPS here? I don't see any reason to do that.
    – JimmyJames
    Commented May 9 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.