473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# MUD/Telnet Server

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
3 26081
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: G520 | last post by:
Hi I have been getting statistical rapports from a machine via a telnet server. Until now it has been done manually. However I want to automate the proccess, and scedule a PHP script to run...
2
by: Dan | last post by:
I'm writing a simplistic telnet client in VB6 and I've run into a small snag. The program has a textbox to write in the string to be sent using ..SendData and has another textbox that displays...
1
by: kumar | last post by:
Hi all, I want to write a telnet server for an embedded linux box using python.The problem is that it doesn't give a shell ( just a limited CLI commands for configuring it . )My requirement is...
3
by: Yannick Turgeon | last post by:
Hello all, I'm currently trying to pass commands to a telnet session and get the texte generated (stdin + stdout) by the session. The problem I get is that the Telnet.read_until() function...
4
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib,...
1
by: luvic.vangool | last post by:
Hi, I am looking for a suitable Ajax Telnet Application. The main reason for this is I have a training company client that needs to allow access to their training systems via completely vanilla...
2
by: thilandeneth | last post by:
i need to do telnet via a web server please give me a idia to initiate the project following requirements are needed 1 Create web based custom telnet client to communicate with remote...
2
by: b00x | last post by:
Hi, I'm trying to develop a script that I can reuse to run remote commands on multiple UNIX servers via telnet. I've tried various php scripts after googling for a good 5 or so hours, but found...
4
by: Patricia Mindanao | last post by:
I want to call cgi perl scripts on my web hosters server from my HTML web pages (on the the web hosters server too). It occurs sometimes (especially during development phase) that these cgi-perl...
6
by: sherrygomindes | last post by:
Hi I have written a perl script using the Telnet module. I need to remotely login in from one windows XP machine to another XP machine. But i get errors which i can't figure out the reason....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.