Jump to content
  • Advertisement
Sign in to follow this  
slayemin

New to network programming

This topic is 2496 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

I'm new to network programming. I'm finding it quite fun. I've started working on a game in my spare time which is going to be multiplayer. I took a class in distributed programming so I have some experience in net coding, but never for a game. I don't know if I'm doing this right, but I've got something up and running and am able to send data back and forth between clients and servers as a proof of concept. I'd like some feedback on whether I'm doing this right or if there are easier ways to do this...

The game is a turn based strategy game built in C# using XNA. Latency isn't going to be an issue. I'm going with a client/server model using TCP/IP as the network protocol. I don't have an internet connection on my development computer, so I can't use the built-in net coding from XNA but I'd like to support it some time in the future. I've decided to create a network "layer" which abstracts out the nuts and bolts of how data is sent. I can switch between Windows Live and the TcpStream methods without having to rewrite game logic. I'm guessing based on sample code that the built-in XNA network code is a lot easier to work with. With the more 'raw' C# network IO, I have to convert all of my data into byte arrays before putting it on the wire.

So, my reasoning is something like this: All of my game objects will need to know how to write themselves to an XML file and read themselves from an XML file. It would be handy if my game objects serialized themselves into XML when they go out onto the network (which they do currently). It would also be handy if I could use the same method for parsing XML files and serialized XML data... but there might be some overhead and pain with that idea. Has anyone else done anything similar? Is there a better way to serialize and send objects over a network?
I'm thinking raw binary could work as long as I know the exact byte sizes of each variable being sent, the order they're being sent, and how many variables I should expect... Currently I'm identifying objects by the opening XML tag, but if I go raw binary, I'd have to use integers and maintain a lookup table of some sort.

I've been working on the server code and I've discovered some interesting problems. First, network coding is really easy when there is only one client and one server... but most multiplayer games have more than two players. So, my server needs a way to broadcast its messages to all of the connected clients. My strategy for broadcasting messages is to create a static list of strings which contains the serialized data I want to transmit to connected clients. When a client connects to my server, it creates a new thread to handle the connection. A part of this newly created thread monitors the static list of broadcast messages to see if it needs to send anything out to the connected client. However, the connected client thread can't delete the message from the list because there's a chance that other connected clients haven't consumed the message yet. So, rather than deleting the message, I create a 'connection id' for each client and associate a list of connection id's with each message. When a client thread consumes a message, it adds its own connection id to the list. The server thread monitors each message and all of the connection ID's which have consumed it. When every connection ID has consumed the message, the server thread deletes the message. However, the interesting problem which comes up is a race condition between threads. So, I add a boolean flag for locking the list when its contents are being changed. Every other thread will have to sit in a spin lock until the last thread unlocks the outgoing messages list. It's all quite a bit of overhead to just send out a few messages. Is there a better way to broadcast messages to connected clients?

On the client side, I'm just maintaining two lists: One for incoming messages from the server, and one for messages it wants to send back to the server. The clients consume the incoming messages, update their game state accordingly, receive user input which changes the game state, and then sends the objects which changed back to the server. There will probably be some sort of server side validation to make sure that the client data is sanitary, but I haven't even begun to think about how I'd do that.

Question: I'm not going to be running a centralized server of any sort. Clients will host a game and become a server, then other clients on the network can find the server and join it. Is there a way for clients to detect available games without pinging every IP on a subnet for an open port? Or, should the server periodically send a broadcast message out to the subnet to announce itself? Also, this might be a REALLY bad idea due to possible network saturation, but could I just let the server broadcast all of its traffic to avoid maintaining broadcast message queues?

Share this post


Link to post
Share on other sites
Advertisement
For local (LAN) server detection, you typically use a UDP broadcast on some known port to find peers.
There are good packages that already do this, such as mDNS.

Share this post


Link to post
Share on other sites
XML seems like an awful lot of overhead for sending a bunch of strategy units. You would certainly be better off encoding them yourself in binary format. Look up the Serializable attribute in C# and some of its uses. If you're willing to change your mind about TCP, lidgren (http://code.google.com/p/lidgren-network-gen3/) is a pretty decent networking library for C#. If you use UDP broadcasts, you can simply encode a message and reliably (with lidgren) broadcast it to every connected client, without having to worry about maintaining a list of who received it.

The client-side looks fine. You do generally maintain incoming and outgoing queues, and you want to group messages together when you send them to avoid needless overhead.

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!