• C# Networking
    7 replies, posted
So after finally switching back to my favorite scripting language, C#, I wanted to dick around with game-related things. I can handle graphics, sounds, saving/loading, but then I hit a brick wall... Multiplayer support. I'm using XNA, for everything, but I do not want to use their multiplayer/net library. Instead I was thinking something more along the lines of the multiplayer in Terraria, which works by many clients connecting to a similar server using the server's IP address. So I began working with that, and I was wondering if I could get some examples of doing so. I found this... [url]http://msdn.microsoft.com/en-us/library/vstudio/system.net.sockets.tcplistener[/url] [url]http://msdn.microsoft.com/en-us/library/vstudio/system.net.sockets.tcpclient[/url] The first is a listener/server, the second is a client. The client connects to the listener/server by the IP and then tells it a message. The listener/server processes the message (Makes it all capitol letters) then sends it back... and then disconnects. I need to find a way to have two or more clients talking to the same server and for the server be able to decipher which client said what and be able to tell only one or both clients something at the same time. Also, no matter what I try, I cannot get the server to function without closing the connection every time, I'm afraid the Microsoft documentation just doesn't explain this clear enough. I'd like some help figuring this out, thanks for ANYTHING in advanced, I'm rather desperate!:smile:
First, you shouldn't use TCP for game networking. This makes things more complicated, but it's better in the long run. [URL="http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/"]This article[/URL] explains why. [URL="https://code.google.com/p/lidgren-network-gen3/"]Lidgren[/URL] might solve your problem. It's a C# UDP networking library for games, and it does a lot of the stuff for you.
After decompiling Terraria, I have seen that TCP and sockets were used. The fact that it is bad to use that doesn't surprise me, there are some very scary things in that code... After decompiling SpeedRunners, I noticed Ligren was used. Thanks, checking those out now! [editline]16th June 2014[/editline] I'm finding examples, but I'd REALLY like to find explanations... Anyway thanks for the help!
[QUOTE=blakeguy25;45127316]After decompiling Terraria, I have seen that TCP and sockets were used. The fact that it is bad to use that doesn't surprise me, there are some very scary things in that code... [/QUOTE] It sickens me that people use TCP for games like Terraria. The article supersnail linked has all you need to know about network programming for a game.
Nothing hands-on. Just the idea behind it all. Searching the website, I found some code written in (what I think is) C++
[QUOTE=blakeguy25;45129215]Nothing hands-on. Just the idea behind it all. Searching the website, I found some code written in (what I think is) C++[/QUOTE] You should be able to read the theory and just use a network library to do the coding, but yeah it does have some code there.
Since I have nothing better to do, I've constructed a small commented example of using UDP in C#. Mind you, you'll have to do all sorts of stuff to use UDP reliably but this just shows you how to communicate in the first place. Sender: [code] static void Main(string[] args) { UdpClient udp = new UdpClient(); // forever while (true) { // wait for input string input = Console.ReadLine(); // convert to byte array for sending (encoding in UTF-8) byte[] packet = Encoding.UTF8.GetBytes(input); // send to 127.0.0.1 (self) on port 23456 udp.Send(packet, packet.Length, "127.0.0.1", 23456); } } [/code] Receiver: [code] static void Main(string[] args) { // listen on port 23456 UdpClient udp = new UdpClient(23456); // start waiting for udp packets udp.BeginReceive(onReceived, udp); Console.WriteLine("Waiting.."); // since BeginReceive is async and does not block, // wait for something so the program doesn't close Console.ReadLine(); } // the function that is called when something happens with receiving udp data static void onReceived(IAsyncResult result) { // comes from passing udp as the second argument to BeginReceive UdpClient udp = result.AsyncState as UdpClient; // this will store the IP endpoint of where the packet came from IPEndPoint sender = null; // read packet and store sender byte[] packet = udp.EndReceive(result, ref sender); // convert packet to string and display string packetString = Encoding.UTF8.GetString(packet); Console.WriteLine("{0} says \"{1}\"!", sender.Address, packetString); //continue listening for packets udp.BeginReceive(onReceived, udp); } [/code] [img]http://i.imgur.com/XEgjcd3.png[/img]
Thanks! Although I'm just going to dick around with Lidgren example and recreate them until I figure out what works and more importantly, what doesn't. That's how I learned to code after all. I decompiled shit, then I looked at what made things work
Sorry, you need to Log In to post a reply to this thread.