Networking

Started by
8 comments, last by Aphton 11 years, 3 months ago

So I am currently making a game and I want to have online multiplayer 1v1. I have never done any type of networking before and I was wondering if anyone could point me in the direction of good tutorials. I have looked and am having a hard time finding anything. I am using C++ and SFML.

Advertisement
How much abstraction do you need? I use wsock2, winsock...
It's great for what I need, but might be too much work for your needs.

I always just stick to raw socket API myself. It's a pretty simple concept at the core level, you open a connection and send strings of data back and forth. JSON is a great format to pass serialized objects over the connection it is pretty easy to encode and decode. To get started search google for the operating system you are targeting, the language and the word "Sockets". This will get you started, once you are successfully working with sockets, sending and receiving information then you will want to start looking in to JSON libraries. Same thing, look for the language you want to use and the word "JSON".

Other people may tell you to use pre built game oriented networking libraries but in actual practice this normally hurts you more than it helps. They impose quite a bit of unseen overhead and require a lot more coding to actually use, and at the core level all networking is simply sending and receiving strings of data. The more you work with raw sockets the more you'll understand it's always better performance to implement your own networking code. (And it's normally < 100 lines of code)

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

(And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.

(And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.

http://www.pcs.cnu.edu/~dgame/sockets/client.c

http://www.pcs.cnu.edu/~dgame/sockets/server.c

Check those out, they are the simplest forms of socket communication examples. Packet division depending on what you mean is handled at the hardware level or at the underlying os level. In short if you send a string you get the string on the other side. You will need to devise a terminating character or sequence so that you can tell what part of your string represents an entire message but this isn't very difficult either. I often use a character that I know will not appear within the actual message (say char 1). When I receive a char 1 break the message, parse the JSON object act and respond. It really is that simple for basic networking.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

SFML provides a network abstraction layer, why not consider using it?

Also, I suggest you check out the multiplayer forum on this site, and read over the FAQ there, it has tons of answers about writing multiplayer games, from no experience to advanced.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

(And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.

To be honest, it is about this number of lines, remember that the server and client in most examples also duplicate some code, (on the reading and sending).

What I suggest is to create your own socket wrapper because it will ultimately simplifiy things and then allows you to solve things in an encapsulated manner (i.e how you deal with Nagle's algorithm etc...)

The great thing about network programming is that it isn't too hard. (Unless I assume, you get to MMORPG levels of things ;)

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

I've thought about this myself and I think it depends on the type of 1v1 game. If it is real-time action, I would suggest researching GGPO and how it has gained its reputation as having the best latency hiding netcode for 1v1 fighting games. http://en.wikipedia.org/wiki/GGPO

I can't find any clear implementation discussions but from the looks of it, it is should be possible to implement it based on the clock synchronization GDNet article here:

http://www.gamedev.net/page/resources/_/technical/multiplayer-and-network-programming/clock-synchronization-of-client-programs-r2493

Basically in GGPO, all keys are sent peer-to-peer with a timestamp and from that timestamp, you can figure out what the peer's current state should be like on your screen at the current time. A history of events will need to be kept for all entities and if something is off by a game-affecting amount, a state in history is chosen and the correction is predicted and applied.

For something like this, you'd basically have to dabble at the socket level.

(And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.

To be honest, it is about this number of lines, remember that the server and client in most examples also duplicate some code, (on the reading and sending).

What I suggest is to create your own socket wrapper because it will ultimately simplifiy things and then allows you to solve things in an encapsulated manner (i.e how you deal with Nagle's algorithm etc...)

The great thing about network programming is that it isn't too hard. (Unless I assume, you get to MMORPG levels of things ;)

I agree, make a simple wrapper class, attach the class to the characters or to the game manager if your using one and voila. Yes you will need some form of a server but the networking itself is all the same. Create a socket, connect or accept it, send and receive and that's all she wrote. Writing advanced servers for say MMO's or large networking social systems or what have you is just a matter of writing the logic based on multiple "socket" connections. Where each socket somehow relates to a "user" or "player".

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post



(And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.
http://www.pcs.cnu.edu/~dgame/sockets/client.c
http://www.pcs.cnu.edu/~dgame/sockets/server.c

Check those out, they are the simplest forms of socket communication examples. Packet division depending on what you mean is handled at the hardware level or at the underlying os level. In short if you send a string you get the string on the other side. You will need to devise a terminating character or sequence so that you can tell what part of your string represents an entire message but this isn't very difficult either. I often use a character that I know will not appear within the actual message (say char 1). When I receive a char 1 break the message, parse the JSON object act and respond. It really is that simple for basic networking.
What I meant with packet division is message-splitting (if thats better to understand).
Im going to try to explain what I really meant further below.
.
Btw the code you provided contains send and recv functions that arent implemented appropriately.
There are 2 problems.
Lets say you wish to send 10 bytes for simplicity's sake.
#1.
x = send(socket, data[0], 10,...)
x <= 0 (either -1 or 0 -> nothing sent; trivial)
x = 10 (everything sent; trivial)
0<x<10 (not everything was sent due to the operating system's internal buffer storing outgoing data being full -->
this implies that one has to track how many bytes were really sent and which need to resend)
#2
lets say send(socket, data[0], 10, ..) returned 10 - so you sent the everything over tcp
x = recv(socket, data[0], 10, ..)
x does not have to be 10!
cases that can happen: x<=0, x=10 or 0<x<10
if 0<x<10 happens, you cant simply assume the data received can be used from now on - because its not a whole "message"/"packet" - its not complete...

"If you send a string you get a string on the other side" This is partially true - you get your string but not in the same shape (damn its really hard to explain =/)
"HelloWorld" 10 bytes: send() may only send lets say 3 bytes -> only "Hel" will be sent
so you need to send the rest by repeating the process
Same happens on the receiving side - you dont have to get 10 bytes or 3.. It can be in different sizes. But its true, you will have your 10 bytes in the end.

Anyway, because of that its essential to implement Streaming-Classes that can store and track how much data has been succeessfully sent & received.

This topic is closed to new replies.

Advertisement