Sign in to follow this  

Basic winsock multiplayer game

This topic is 2042 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:

[code]
struct PlayerState
{
float x;
float y;
float angle;
bool bulletCreated;
};
[/code]

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 127.0.0.1. 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 [url=http://www.mindcontrol.org/~hplus/graphics/game_loop.html]Canonical Game Loop[/url].
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

This topic is 2042 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this