• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JuliusDegutis

Simple network game

5 posts in this topic

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?

 

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

- 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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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 :)

-1

Share this post


Link to post
Share on other sites

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  
Followers 0