Simple network game

Started by
4 comments, last by JuliusDegutis 10 years, 7 months ago

Hello, writing here because I'm kinda stuck in understanding how to make network game...

I making my game engine from zero (yup, I'm stupid, because don't use engines which is made already, but just want to learn more basics about everything). Have made simple movement by input and with collision. Now I want to make I could "play" with my roommate.

What I want to make: game client could be server too. For example I launch game and my roommate connect to me (via my IP), and I would be server, and him would be client. As I move somewhere, my world position would be sent to all clients, and painted. As client moves, his position would be sent to server, and then echo to other clients and painted.

I started from making demo chat program for data (messages) sending. But without any good results. I have made server, which could receive only from one client, and can't send. When trying to make read from more then one, it stucks on accept, because for now used blocking sockets. Trying using C++ with winsocks2 (and for game using directx9).

What I think classes would have:

Client:

connect() {simple connect to server via ip}

send() {send data to server, and server echo to other clients}

receive() {get data from server}

sendToServer() {send data only to server}

disconnect(){send message to server and disconnect}

Server:

send(){send data to client}

receive(){get data from client/s}

echo(){send data to all clients}

kill(){send message to client to disconnect it}

maybe somebody could show any examples or what to read to understand better what I need?

Advertisement

The basic Winsock sockets are blocking, both on accept(), recv(), and to a certain extent send(). That means that you can't do more than one thing at a time with a single thread. There are multiple solutions:

- have a master thread which contains the server socket which listens for incoming connections and accept()'s them, and spawn a new thread for each client that connects. The advantages is that it is straightforward and gets the job done, but the downside is that it does not scale with more than a few clients.

- use asynchronous, event driven sockets (available in Winsock with no particular library required) which basically fire an event every time something of interest happens on the network, very comfortable to use as you can put those events into a queue and pick them up in your game loop, without needing threads, and letting Winsock do all the heavy lifting for you (you'll probably need a little network class to parse those raw events into something your game can use, though). It is very flexible as well.

- use IOCP, which is an advanced technique which is very efficient and scales extremely well, unfortunately it is a pain to implement and you will probably want to use a preexisting library. However, IOCP is not really meant for stuff like games where people stay connected for a long time and send lots of data, though it can be made to work - it is more for web services which need to handle up to thousands of simultaneous (lightweight) requests, things like that.

In your case, I would suggest giving event-driven networking a shot. It feels alien if you've only ever done blocking networking, but it can be liberating. Don't just into your game with it, though, get comfortable with it by writing prototypes and samples before.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Winsock with select() is not blocking more than what you tell select() to block.

recv() and send() and accept() are guaranteed to not block the next time you call them if select() says the file descriptor is ready.

Thus, you can easily write a single-threaded program to handle up to 64 sockets (including the listening socket, for TCP) on Windows using select(), and 1024 sockets on UNIX using select(). (the 64/1024 values come from the default sizes of the fd_set structure.)

Windows event-based sockets perform poorly, have gotchas, and are not easily portable. They may have been a good idea on Windows 3.11, but you really shouldn't use them these days.

High-throughput servers on Windows typically use IOCP with the system thread pool, and high-throughput servers on UNIX typically use kpoll or similar (different for each flavor) with manually managed pthreads (at most one per core.)

The main thing to worry about at the initial level is that send() and recv() do *not* deal with packets with TCP -- you have to frame your outgoing packets in a length+data frame, and the incoming recv() code has to first read the length, and then wait until that much data is available until actually acting on an incoming packet. Typically, this includes some kind of application-level buffering.

enum Bool { True, False, FileNotFound };

- use asynchronous, event driven sockets (available in Winsock with no particular library required) which basically fire an event every time something of interest happens on the network, very comfortable to use as you can put those events into a queue and pick them up in your game loop, without needing threads, and letting Winsock do all the heavy lifting for you (you'll probably need a little network class to parse those raw events into something your game can use, though). It is very flexible as well.

I think I have implemented this one now. And on my game engine. Now server can send to all client message, and all receives it. And client could send message to server.

But came up with "bug". I think because don't know something: if my roommate creates server, I can connect. But if I create server, then roommate gets 10060, that connection timeout. For server and client using same port 5566. If I can connect to him, then this port is ok? But then he connect, it fails? Could it be?

First, I highly advise against using the event driven sockets on Windows. They are known to be finicky; they perform poorly; they have a bunch of caveats; and they are not portable.

Second, we need to know more to understand what the situation is. Are you and your room-mate both on the same physical network? Do you, or your room-mate, have some kind of firewall running? Is there a router of some sort involved?

The problem you describe sounds like either a problem with Windows Firewall, or with NAT.

enum Bool { True, False, FileNotFound };

First, I highly advise against using the event driven sockets on Windows. They are known to be finicky; they perform poorly; they have a bunch of caveats; and they are not portable.

Second, we need to know more to understand what the situation is. Are you and your room-mate both on the same physical network? Do you, or your room-mate, have some kind of firewall running? Is there a router of some sort involved?

The problem you describe sounds like either a problem with Windows Firewall, or with NAT.

For now will try event driven, and if that won't satisfy me, then will try something else ;)

And thanks for second, because turned off firewall, and connected easily :)

This topic is closed to new replies.

Advertisement