By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,698 Members | 1,328 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,698 IT Pros & Developers. It's quick & easy.

C# MUD/Telnet Server

P: n/a
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!
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 17 '05 #2

P: n/a
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
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).
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
Nov 17 '05 #3

P: n/a
On Wed, 24 Aug 2005 10:01:02 -0700, "Benny"
<Be***@discussions.microsoft.com> 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 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.
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.