C# MUD/Telnet Server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 etc. Thanks in advance!
 
Benny said:
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.
 
Benny said:
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
etc. Thanks in advance!

Oops, a couple warnings regarding my previous post: there needs to be an
exception handler in ClientLoop() that catches all exceptions, and I also
needed to warn you to never, ever read from a socket except in the one place
in ClientLoop() that already does so. An attempt to do so will freeze the
server until the read is complete. Instead, remember any state that you have
to and allow it to return back to ClientLoop() (essentially this is a
single-threaded strategy).
 
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 etc. Thanks in advance!

You may also want to look into porting C/C++ code to C#. I saw on the
Net someone who was doing it. Porting is not that difficult, but you
also want to be familiar with .Net Framework pretty well, so you don't
end up porting things that have already been implemented. (Especially
networking, threading and data structures)

There is also a book called "MUD game Programming" by Ron Penton. It
focuses on C++, but you can apply it to C# as well with a very little
extra effort. It's been floating around in a CHM form (Compiled HTML
Help format) in groups like alt.binaries.e-book,
alt.binaries.e-book.technical and such. Or you can find it in book
stores or Amazon.com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top