Jump to content
  • Advertisement
Sign in to follow this  

Basic winsock multiplayer game

This topic is 2410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm a newcomer to Winsock. I understand the concepts of sockets, ports blocking/non blocking, and I have made a simple chat program. Next I wanted to try making a very very simple game with 2-player online multiplayer. Here's how the game should be:
both players each control a 2D tank from a directly-above perspective. They use WASD to move and the mouse to aim and shoot:

The server is a console application, which should be designed like this:

- Initialise WSA and create a listener socket
- Block on accept() until a client connects
- Block on accept() until another client connects
- When both players have connected, tell them to start the game
- Start loop:
- recv() a PLAYER_STATE struct from player 1, (or recvfrom(), depending on whether I use TCP or UDP
- recv() PLAYER_STATE from player 2
- send() player2's state to player 1
- send() player1's state to player 2
- End loop:
- shutdown() etc

Here is the PLAYER_STATE struct:

struct PlayerState
float x;
float y;
float angle;
bool bulletCreated;

The client is then designed like this

- Initialise windows, graphics, sound etc
- get the player to input the server's IP, using the console (If the client is on the same computer, use It might also be useful for the server to printf() it's IP)
- Initialise WSA, create a socket, try to connect() to server
- wait for start game message from server
- start loop
- send() state to server
- recv() state of other player
- draw graphics (with 60fps vsync), update input etc
- modify internal state based on input, other player's state etc
- end loop
- cleanup

So is this an adequate design for a very very simple 2D game? If I can get it working then I think I will extend the same concepts to a 3D shooter

Share this post

Link to post
Share on other sites
You should de-couple the send/receive part from the drawing part. Check out this link to the Canonical Game Loop.
Note that that loop also works for network games, by changing the loop on top to do both get all UI events, and empty the incoming network pipe (if there are any messages there). Specifically, you do not want to block on the network, just poll it to attempt to drain *any* messages (may be more than 1).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!