for those who asked for a mud client code

Nights!

This evening I've just bored, so I wrote a small, simplest mud client in c# and .net.

There is a text box for the output, another for the input, and a button to connect (by default to alteraeon.com, I haven't played muds for a long time, and it was the first one I remembered). I supose it's not visually friendly, but it's readable with a screen reader.
feel free to modify, reuse code, ask anything, or whatever.

Minimum functions as follows:
-Use the connect button to connect to the server
-Press enter to send text to the server.
-Navigate to the other textbox to read output.
I used visual Studio 2013 Community to code and compile.
And it's all. Download all the sample (including a compiled version and source code) from:

https://www.dropbox.com/s/s9j7ncmuvyd5q … 1.zip?dl=1

The compiled version is in mudclient1/bin/debug/mudclient1.exe, or somewhat... tongue
If you are just curious, you can read the main code with some comments below
If you find bugs and possible improvements,
just take that as your next exercise! No idea if I will improve it anymore smile

And now, some code:

using System.Net.Sockets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mudclient1
{       
    public partial class        Form1 : Form
        {
        // Fields needed
        private NetworkStream stream;
            private TcpClient tcpClient = new TcpClient();
            private Timer MainTimer;
        //Constructor
        public Form1()
        {
            InitializeComponent();
            // creates a new instance of the timer class and setts its interval property
            // this timers receives data and refreshes the screen
            MainTimer = new Timer();
            MainTimer.Interval = 1600;
            // the event for the timer
            MainTimer.Tick += MainTimer_Tick;
                   }
       
        // This method writes to the networkstream
        private void WriteIn(string input)
        {
            byte[] writeBuffer;
            input += Environment.NewLine;
            if (stream.CanWrite)
            {
                writeBuffer = System.Text.Encoding.ASCII.GetBytes(input);
                stream.Write(writeBuffer, 0, writeBuffer.Length);
            }
        }

        // This one reads from the network stream
        private string ReadOut()
        {
            if (stream.CanRead)
            {
                byte[] myReadBuffer = new byte[1024];
                StringBuilder myCompleteMessage = new StringBuilder();
                int numberOfBytesRead = 0;
                                // Incoming message may be larger than the buffer size.
                do
                {
                    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
                                        myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                }
                while (stream.DataAvailable);
                              return  myCompleteMessage.ToString();
            }
            else
            {
               return "Error: Cannot read from the network stream.";
            }
        }
       
        // This method connects to the server
void Connect()
{
     if (tcpClient.Connected)
     {
         MessageBox.Show("You are already conected to the server.");
         return;
     }
     try
     {
         // Initialize TCPClient object and connect
             tcpClient.Connect("alteraeon.com", 3000);
         //    Initialize networkStream and set timeout property
         stream = tcpClient.GetStream();
stream.ReadTimeout = 2000;
         // writes received data to the screen
         WriteLine(ReadOut());
         // let's launch our timer
         MainTimer.Start();
             }
         // catching exceptions
     catch (SocketException ex)
     {
         WriteLine("Socket Error:" + ex.Message);
     }
     catch (Exception ex)
     {
         WriteLine("ERROR:" + ex.Message);
}
}
       
        // This method prints text to the screen
     private void WriteLine(string text)
{
    screenTextBox.Text += text;
}

        // event fired when the button is activated
private void button1_Click(object sender, EventArgs e)
{
    Connect();   
}
       
        // this event checks if enter key is pressed and sends data from the text box to the server
private void inputTextBocx_KeyDown(object sender, KeyEventArgs e)
{
            if (e.KeyCode == Keys.Enter)
    {
     WriteIn (inputTextBocx.Text);
                // let's clear the text box and prevent any other action
                inputTextBocx.Clear();
        e.SuppressKeyPress = true;
    }
    }

        // method fired for each tick of the timer
private void MainTimer_Tick(object sender, EventArgs e)
{
    // if there is data available
    if (stream.DataAvailable)
    {
        WriteLine(ReadOut());
    }
}

       
       
    }
}

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector

Reply via email to