Working on a little networked game. Need a little input.

Started by
3 comments, last by kriskramer 18 years, 8 months ago
Howdy. I started working on an idea for a networked game about a year ago, and actually managed to develop quite a bit of the gameplay (through C#) before realizing that I hadn't figured out the networking side of things. So, I basically started over and am rebuilding the game from scratch (sort of) and learning network programming as I go. So far, I've managed to find some basic socket methods that work for basic things like logging on, loading characters, viewing items, etc. Basic 'here's my request, send me the info' kind of stuff. Now, I also have a real combat piece in the game, that I know this will not be useful for, but I'm putting that off for the moment while I make sure what I do have is working correctly. What I'm mostly hoping for is any suggestions anyone may have that would help me avoid problems before they happened, rather than learning the hard way. :) Here's the code I'm using for the server app: public static void StartServer() { // Establish the local endpoint for the socket IPHostEntry ipHost = Dns.Resolve("localhost"); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11001); // Create a TCP/IP socket Socket sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and // listen to the incoming sockets try { sListener.Bind(ipEndPoint); sListener.Listen(10); // Start listening for connections while (true) { // Program is suspended while waiting for an incoming connection Socket handler = sListener.Accept(); string data = null; byte[] bytes = new byte[2048]; int bytesRec = handler.Receive(bytes); data += Encoding.ASCII.GetString(bytes,0,bytesRec); string reply = "00000"; Server1.Packet packet = new Packet(data); [ this is where I have a switch statement which determines which methods to call based on the data received. I'll post it if anyone wants to see it but since it wasn't directly related to the network stuff, i took it out to keep things less cluttered.] byte[] msg = Encoding.ASCII.GetBytes(reply); handler.Send(msg); handler.Shutdown(SocketShutdown.Both); handler.Close(); } } catch(Exception e) { Console.WriteLine(e.ToString()); } } It works well enough, but I was hoping to have a server app that I could interact with rather than JUST handle incoming requests. Here's the method I use on the client to send data: public static Client1.Packet SendRequest(string data) { Client1.Packet packet = new Packet("^" + (int)Packet.PacketCodes.Login + "^0"); IPHostEntry ipHost = Dns.Resolve("localhost"); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11001); Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); login = 0; try { sender.Connect(ipEndPoint); byte[] bytes = new byte[2048]; byte[] msg = Encoding.ASCII.GetBytes(data); int bytesSent = sender.Send(msg); int bytesRec = sender.Receive(bytes); string result = Encoding.ASCII.GetString(bytes, 0, bytesRec); packet = new Packet(result); if (sender.Connected) { sender.Shutdown(SocketShutdown.Both); sender.Close(); } return packet; } catch(SocketException se) { Console.WriteLine("Socket Exception:" + se.ToString)); } return packet; } And here is the Packet class I created to capture/decode the data on each side (it's still rough since I haven't decided what all it needs to do): public class Packet { public Packet() { // // TODO: Add constructor logic here // } public enum PacketCodes { Other, Login, Characters, NewUser, LoadChar, RecvChar, BuyItem, SellItem, TOTAL_PACKETCODES } public Packet(string data) { if (RawData == null) RawData = data; ParseData(); } public string RawData; public string User; public PacketCodes Code; public string[] Data; public void ParseData() { if (RawData != null || RawData.Length > 0) { char[] seps = "^".ToCharArray(); string[] items = RawData.Split(seps); User = items[0]; Code = (PacketCodes)int.Parse(items[1]); Data = new string[items.Length - 2]; for (int i = 2; i <= items.GetUpperBound(0); i++) { Data[i-2] = items; } } else { User = ""; Code = PacketCodes.Other; } } } Really, I'm just hoping to know if I'm on the right track. It works for the basic stuff that I need to do, and I'm of the opinion that if it works, there's no need to overhaul it for something with only slightly more usefulness. But any suggestions are welcome, as I still know nothing about socket programming. :) Also, I can post any other code you guys want to see. Just let me know.
Advertisement
Try the Forum FAQ. It has answers!
enum Bool { True, False, FileNotFound };
Yeah, I read it. But I didn't see anything that directly applied to what I was looking for. That's why I posted. :)

I was hoping to get personal feedback/experiences from people who have done something similar rather than just read canned exposition, or spend a few weeks studying books that don't really seem to explain what it is I'm looking to do...
Kris,

I find myself in a rather similar situation as you do so I can't really make any suggestions as to how you could improve your code or what problems you might run into. In fact, I'm about to take your code and see if I can make it work for me as I find the networking aspect still rather incomprehensible.

However, the part where you say:
"It works well enough, but I was hoping to have a server app that I could interact with rather than JUST handle incoming requests."

I'd say that if you wanted to interact with your server, you probably have to write an 'admin'-client which allows you to send requests to your server to change certain settings rather than perform those actions on the server process itself.

I hope that made any sense... it does in my head *grin*

For what it's worth,

Gypsy
I actually had the same idea you just brought up, that of an admin-client, but I did some more poking around and came up with another example client-server app that works better than what I posted above. I got it from the following link:

http://www.codeguru.com/Csharp/Csharp/cs_network/sockets/article.php/c8781/

and it allows me to do a bit more than I could with the old code. I'm in the process of modifying the client to run as a library rather than a startup form, so I can attach my game forms to it that way.

If you want, let me know and I'll share what I've learned so far while doing all this stuff. :)

This topic is closed to new replies.

Advertisement