473,529 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26092
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
6181
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 everyday. Using my terminal, I give the command to order a rapport, and then press <ESC>. Then the rapport is printed out.
2
50259
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 what that server sends. When I first connect to the server (in this case, my university's smtp server), I get a response that the server acknowledges...
1
3821
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 to write a telnet server ( residing on the box) listening on some specific port where it can listen to telnet requests from clients and provide them...
3
8645
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 seems to freeze after a couple of command. I did a simplify script that reproduce the problem each time (I'm using 2.3.4 on W2K):
4
11137
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, this code returns the traceback that follows: import telnetlib host = 'nnn.nnn.nnn.nnn'
1
5886
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 browsers, and does not want the trainees to have to install anything, or accept anything, on their machines. My requirements are that it: 1) has...
2
5167
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 destinations. must provide login security before make communication 2 telnet communication should not be a direct 1 to 1 communication and that must be as...
2
15747
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 no luck. I've created a script (suprisingly) similar to the on found an old post from 2001...
4
2946
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 scripts didn't work like intended. They crash because e.g. - syntax errors - wrong or changing pathes - unexpected user input - ...
6
5492
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. Please someone help me its very very urgent. here is my code: #!/usr/bin/perl -w
0
7345
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7269
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7671
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7614
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5809
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
902
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.