Benny wrote:
I have been looking into developing a MUD in C# and most of the help
out there is for C++. I would prefer C# so I was hoping someone
could point me in the right direction. I just need a jump start on
how to develop a telnet server to respond to asynchronous commands
Hi Benny. MUD programming is one of my specialty areas, so please feel free
to ask any other questions you have on the topic. I'll start with one strong
suggestion: make it single-threaded. This will avoid heaps of complexity and
bugs and it should run fine for a small number of players (<100).
Making a multiplayer server single-threaded is a bit tricky though. Instead,
it's easier to have a thread for each player, but have a single Big Lock
around all the processing. Here's a complete sample, the world's tiniest MUD
in C#:
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SimpleMUD
{
class Server
{
const int PortNumber = 4000;
const int BacklogSize = 20;
static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, PortNumber));
server.Listen(BacklogSize);
while (true)
{
Socket conn = server.Accept();
new Connection(conn);
}
}
}
class Connection
{
static object BigLock = new object();
Socket socket;
public StreamReader Reader;
public StreamWriter Writer;
static ArrayList connections = new ArrayList();
public Connection(Socket socket)
{
this.socket = socket;
Reader = new StreamReader(new NetworkStream(socket, false));
Writer = new StreamWriter(new NetworkStream(socket, true));
new Thread(ClientLoop).Start();
}
void ClientLoop()
{
try
{
lock (BigLock)
{
OnConnect();
}
while (true)
{
lock (BigLock)
{
foreach (Connection conn in connections)
{
conn.Writer.Flush();
}
}
string line = Reader.ReadLine();
if (line == null)
{
break;
}
lock (BigLock)
{
ProcessLine(line);
}
}
}
finally
{
lock (BigLock)
{
socket.Close();
OnDisconnect();
}
}
}
void OnConnect()
{
Writer.WriteLine("Welcome!");
connections.Add(this);
}
void OnDisconnect()
{
connections.Remove(this);
}
void ProcessLine(string line)
{
foreach (Connection conn in connections)
{
conn.Writer.WriteLine("Someone says, '" + line.Trim() +
"'");
}
}
}
}
Fire it up and connect to port 4000 - it's a simple chat server. You can add
whatever you want to OnConnect(), OnDisconnect(), and ProcessLine() to
extend the server. These calls happen under the Big Lock, so you're safe
against all the hazards of multithreading. This code isn't very well tested
though, so proceed at your own risk.
I also strongly suggest you use the XML support in order to load/save world
data; strongly-typed datasets may also be helpful here. Please write back if
you need more information. I hope this helps.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at
http://www.microsoft.com/info/cpyright.htm