Handmade Hero»Forums»Code
100 posts
General discussion over networking

I don't know whether Casey has a plan for multiplayer or something similar to some of the zachtronics games where people put scores, or something. He probably doesn't but I got curious.

In that case, we would need to use networking.

But networking seems all over the place in C/C++. There is no basic standard, and personally I do not like the boost libs tho they have the general-purpose-multi-threading ASIO that does a lot of stuff.

How would you guys approach a networking layer for a game like HMH, IF you wanted to make a simple thing like playing with a friend in some 2-player module of the game?

In particular, this would be like building a basic "chat"-like module, right?

Where is the best place to know how to spin this in moderately simple terms? Something that would give me a suitable socket (or perhaps just having the raw socket programmed in extra sources).

Especially, if one of you guys ever worked with networking like this, what did you use? What are good references on that?

Thanks folks!

1 posts
General discussion over networking
Edited by owntempo on

You might want to take a look at the Networking Chapter in the book Hacking The Art of Exploitation. I found it way better than many other resources because it went straight into code, writing a server, and since its about hacking it explains a lot of subtle ideas and behaviours. If you like low level programming you might also enjoy other chapters.

100 posts
General discussion over networking
Replying to owntempo (#26288)

I got the pdf and started to read the sockets section, it reads very down to earth non-bullshit so I'll have to thank you for this recommendation!

Hoping to get other recommendations.

6 posts
General discussion over networking

To communicate between you and another computer over the Internet you use sockets. Sockets generally come in two flavors: TCP or UDP. UDP is a better choice for games, but you have to build in whatever reliability checks your game needs. For more information on sockets / networking more generally see here: https://beej.us/guide/bgnet/html/

In terms of the actual networking algorithms for synchronizing state, this is a good article series: https://gafferongames.com/post/introduction_to_networked_physics/

Although incomplete, this series might also be helpful to you: https://www.codersblock.org/blog/multiplayer-fps-part-1

On Windows you can use WinSock which has almost the same API as the Unix socket library with slight differences.

Then the general idea is the following:

  1. On init, create a new UDP socket and set it to non-blocking
  2. If you're the server, bind the socket to a particular address / port so others can find it
  3. If you're the client, connect to the server's address

To listen for new messages:

  1. Every frame you listen for new incoming socket events with recvfrom
  2. If you got a message, make sure it's valid and parse it

To send a message:

  1. Call sendto with the destination address

Note that UDP makes no guarantee that:

  1. Your messages will be delivered in order
  2. Your messages will be delivered at all
Mārtiņš Možeiko
2559 posts / 2 projects
General discussion over networking
Replying to nickav (#26391)

UDP is a better choice for games

Depends on game type.

If you're doing turn based strategy game, or card game or whatever that does not depend on real-time data, then the TCP is perfectly fine. Or sometimes just regular https GET/POST requests is enough.