Tuesday, July 6, 2010

How to: Lidgren Network

Lidgren Networking
Homepage: LidgrenHome

Lidgren Network is quite easy to use networking library. It can be used for anykind of software.
I started to work with this library about month ago. Before that, i had never ever done anything that relates to networking.

Setting up chat server was quite easy. After that, i started to think about game server architechture, handling of multiple different kinds of packets, worldState handling, client updating, bandwidth usage.. To be honest, its big task, to build working multiplayer game server and client. You have to handle ever growing amount of different kind of packets. Keep updating clients, with data they need etc. It took me a while to understand all this.
I thought first, that internet must be full of example code, but no.. it's not. Atleast i did not find any. Or if i did, it was so professional code, that i could not even read it.

Things are about to change :)

I created small console based "game", that works over internet, but you can test it on your localhost. When it ask's for IP, just type "localhost". It's very simple example and i tryed to comment it, but feel free to ask help, if something is confusing.
This example covers the very basics of game networking. Message handling, movement, server world state handling etc. Hope you learn something from it.
Here it is: Game Network Example

31 comments:

  1. Thank you very much for your example!

    ReplyDelete
  2. Impressive! Maybe write a step-by-step tutorial to not overwhelm beginners?

    ReplyDelete
  3. YOU ARE GREAt! ITS FIRST REAL CODE AND TUTORIAL TO LIDGREN I LOVE YOU XD HAHA THKS

    ReplyDelete
  4. OMG^^ I love you xD I had a lot of ideas how to handle the diffrent kind of packets i have to send and recieve in my game but none of it works fine and now...finally i found your your code with great comments for every step!! And so it was realy easy to made my game playable over internet! I have to say it again I LOVE YOU XDD

    ReplyDelete
  5. Thanks very much, after searching the web for a couple hours for a starting point to game networking (in XNA), Lidgren + this sample seems like a really good place to start.

    ReplyDelete
  6. Thank you! Your a life saver! This is perfect! Easy to understand and quick to get started. Great job.

    ReplyDelete
  7. i'm downloading your code and start running with XNA4.0 i face with the window that contain
    "server started"
    "Waiting for new connections and updateing world state to current ones"

    Ps. Sorry i'm absolute beginner in network please tell some step for me

    ReplyDelete
  8. Try to right click on Client project -> debug select start new instance

    ReplyDelete
  9. This is great, thanks. Just one question. where do you set the IP address the server is listening on?

    ReplyDelete
  10. Hey thanks for this, going to help me out tons.

    For the guy above me, the IP is that of the computer that is running it. If you're running both the server and the client on the same IP, then you can use the "Home" IP which is 127.0.0.1. The port is defined in the server code but you don't need to change this.

    ReplyDelete
  11. Great job Riku! This will help me make a multiplayer racing game on the PSS platform. I'll remember you when things go good with this :D. /cheers
    Dan.

    ReplyDelete
  12. Thanks! This will help a lot.

    ReplyDelete
  13. This was of great help!

    ReplyDelete
  14. How do I get the IP of the host computer to be able to connect too it? I've tired the one on whatismyip.org but it doesn't connect? Any ideas?

    ReplyDelete
  15. Cheers for this - it has been really helpful!

    I was just wondering what to type when it asks for an IP address if I'm trying to have the server on one machine and the client on another? Localhost works fine but I can't figure out what is needed to connect between 2 machines!

    ReplyDelete
  16. Warning 1 The primary reference "Lidgren.Network" could not be resolved because it has an indirect dependency on the framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0,Profile=Client". To resolve this problem, either remove the reference "Lidgren.Network" or retarget your application to a framework version which contains "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". XnaLidgren

    This is what i get as a warning when i import the library in an xna game project. I thought that it would not conclude to any other errors but when i try to even insert the classic using Lidgren.Network; and i get an Error 5 The type or namespace name 'Lidgren' could not be found (are you missing a using directive or an assembly reference?). Cannot really conclude to what i should fix to get this working as i have never entered into networking with lidgren. Can comeone please advice me as what i have to do to fix this. Thanks in advance.

    ReplyDelete
  17. http://msdn.microsoft.com/en-us/library/vstudio/cc656912(v=vs.100).aspx

    You must change the profile target away from client profile.

    http://msdn.microsoft.com/en-us/library/vstudio/bb398202(v=vs.100).aspx

    ReplyDelete
  18. Awesome job! Well done helped me a lot! Thanks!

    I will try to upgrade it to an xna working example.

    ReplyDelete
    Replies
    1. would be awesome if you'd share that example :)

      Delete
  19. Interesting, at first I didn't understand that the A,S,W,D was being sent back to the client over the network. It look like it was not working.....might want to clarify that?

    ReplyDelete
  20. I am receiving a "Strange Message" error.
    Any workarounds?

    ReplyDelete
    Replies
    1. Keep getting the same message too. I'm trying localhost and open my ports. Did someone know what happens here?

      Delete
  21. For some reason I cannot get this to work from 2 PC's across the internet....do I need to open the port on my router or firewall ??

    ReplyDelete
  22. It works, as long as the Server side uses Port Forwarding UDP, for the assigned port. Works very well !!

    ReplyDelete
  23. Hey, I gotta say, your example is dead simple and straight forward. it has been really helpful in thinking of how to parse msgs.

    ReplyDelete
  24. Glad I finally found your page -- a nice example that explains everything I need to get started. Thanks

    ReplyDelete
  25. Nice place to start learning lidgren.

    This works when I'm connecting clients and the server in one computer, but I want to able to connect with two computers.

    Tried opening a client for both computer and a server in one of them but that doesn't work, do I need to change something in the code or is there an external problem?

    ReplyDelete
  26. Thanks for your example, it's very-very helpfull

    ReplyDelete
  27. Thanks for this! :)

    ReplyDelete