[Tutorial] Simple Command Prompt Responder

Transcribe
by Transcribe · 22 posts
11 years ago in .Net (C#, VB, etc)
Posted 11 years ago · Author
This is meant for C#

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab2TakeInputData
{
    /* This program takes input of name
     * it then takes age input
     * Finally displays it as one statement
     */
    class Program
    {
        static void Main(string[] args)
        {
            // this line displays enter your name
            Console.WriteLine("Enter your name ");
            // this line takes input from the end user
            string SomeDatafromUser = Console.ReadLine();
            try
            {
                // this line checks if the name is not greater than 10
                if (SomeDatafromUser.Length > 10)
                {
                    throw new Exception("Your name should not be greater than 10 characters");
                }
                // this line displays enter your age text
                Console.WriteLine("Enter your age");
                int Age = 0;
                // this line takes age input
                Age = Convert.ToInt16(Console.ReadLine());
                // this line displays the complete statement with data entered
                Console.WriteLine("Hello Mr " + SomeDatafromUser + " your age is " + Age);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}
Posted 11 years ago
I miss C++ I learned it when I was in school, then I left school and forgot it.
Posted 10 years ago
nice, lets try different things
I recreated your program using some object oriented concepts
im opent to questions

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab2TakeInputData
{

 class User // our class
    {
        private string Name; // the name field
        private int Age; // the age field

        public string UserName // the Name property
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        }

        public int UserAge // the age property
        {
            get
            {
                return Age;
            }
            set
            {
                Age = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            User ImvuMafiasUser = new User(); //creates a new object of class user

            // this line displays enter your name
            Console.WriteLine("Enter your name ");
            // this line takes input from the end user
            ImvuMafiasUser.UserName = Console.ReadLine(); //assign the name to our object's name
            try
            {
                // this line checks if the name is not greater than 10
                if (ImvuMafiasUser.UserName.Length > 10)
                {
                    throw new Exception("Your name should not be greater than 10 characters");
                }
                // this line displays enter your age text
                Console.WriteLine("Enter your age");
               
                // this line takes age input
                ImvuMafiasUser.UserAge = Convert.ToInt16(Console.ReadLine()); //assign the age to our object's age
                // this line displays the complete statement with data entered
                Console.WriteLine("Hello Mr " + ImvuMafiasUser.UserName + " your age is " + ImvuMafiasUser.UserAge);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }


}
Posted 10 years ago
Anyone care to explain what a Command Prompt Responder is?

A command prompt Responder is simply a program that displays and accepts text through a command prompt.
In this case, the program accepts a person's name and age.
Then it displays the user's name and age back to them in a full sentence.
Posted 10 years ago
thank you Don Von.
I think it makes more sense to rebuild this program as a client-server application. I can do that if anyone is interested
Posted 10 years ago
Elie wrote:
thank you Don Von.
I think it makes more sense to rebuild this program as a client-server application. I can do that if anyone is interested


Would the server side application be in C# or in a scripting language?
Posted 10 years ago
Don Von Free Credits wrote:
Would the server side application be in C# or in a scripting language?



actually I had C# in mind as we're in the C section
but I can make another version using scripting languages if you like.
Posted 10 years ago
Elie wrote:
Don Von Free Credits wrote:
Would the server side application be in C# or in a scripting language?



actually I had C# in mind as we're in the C section
but I can make another version using scripting languages if you like.


Which ever you think would be the most useful. I am not sure how to run c# on our server, nor the effect it would have on our server load.
Posted 10 years ago
sorry, for the late reply .. i had a very busy month >.<

Which ever you think would be the most useful. I am not sure how to run c# on our server, nor the effect it would have on our server load.

I wasn't thinking of running the server online, lets just run it locally first

I just wrote this code and tested it and please feel free to ask me, we need to open two project A. a server , B. a client

Our Server
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            int recv;
            byte[] data = new byte[1024];
            string[] input = new string[2];
            string stringData;

            IPEndPoint ipep = new IPEndPoint(IPAddress.Any,9050); // Defining the IP and port we're gonna use to listen to clients
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Preparing our socket to use TCP protocol
            newsock.Bind(ipep); // bind the socket to the ip & port
            newsock.Listen(10); // listen to Clients

            Console.WriteLine("Waiting for a client...");
            Socket client = newsock.Accept(); // once a client connects, accept it and return a socket
            IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint; // obtain client info
            Console.WriteLine("Connected with {0} at port {1}",clientep.Address, clientep.Port); // displaying info

            string welcome = "Welcome to my test server"; // preparing a welcome message
            data = Encoding.ASCII.GetBytes(welcome); // encoding it into bytes
            client.Send(data, data.Length, SocketFlags.None); // send to client

            // Conversation with Server could be in a while loop, in our example its just sending name and age and recieving it
            ////////////////////////////////////////////
                data = new byte[1024];
                recv = client.Receive(data); // recieve from client
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                input = stringData.Split(','); // split the message when seeing ','
                stringData="Hello " + input[0] + " your age is " + input[1]; // re create the message such as : Hello X your age is Y

                data = Encoding.ASCII.GetBytes(stringData);
                client.Send(data, data.Length, SocketFlags.None); // send to client
            ////////////////////////////////////////////

            Console.WriteLine("Disconnected from {0}",
            clientep.Address);
            client.Close();
            newsock.Close();
        }
    }
}

Our Client
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[1024];
            string input, stringData;

            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);// Defining the IP and port we're gonna use to listen to connect to the server
            // 127.0.0.1 is loopback localhost , so if you are going to run this different machines you need to replace it with the IP of the other machine running the server
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);// Preparing our socket to use TCP protocol

            try
            {
                server.Connect(ipep); // trying to connect to server
            } catch (SocketException e) // if failed
            {
                Console.WriteLine("Unable to connect to server.");
                Console.WriteLine(e.ToString());
                return;
            }

            int recv = server.Receive(data); // get the welcome message from the Server
            stringData = Encoding.ASCII.GetString(data, 0, recv); // Encode it
            Console.WriteLine(stringData); // Display it

            // Conversation with Server could be in a while loop, in our example its just sending name and age and recieving it
            ////////////////////////////////////////////
                Console.WriteLine("Enter your name "); // enter name
                input = Console.ReadLine();
                Console.WriteLine("Enter your age "); // enter age
                input += "," + Console.ReadLine(); // form them in one output
               
                server.Send(Encoding.ASCII.GetBytes(input)); // send to server

                data = new byte[1024];
                recv = server.Receive(data); //recieve from server
                stringData = Encoding.ASCII.GetString(data, 0, recv); //encode to string
                Console.WriteLine(stringData); // display
            ////////////////////////////////////////////

            Console.WriteLine("Disconnecting from server...");
            server.Shutdown(SocketShutdown.Both);
            server.Close();
        }
    }
}



Image

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT
Select a forum Protection     Help & Support     Introductions     Mafia News     IMVU News General Discussion     IMVU Lounge        IMVU Series / Roleplaying        Social Games     Mafia Market     Mafia Tools        Premium IMVU Tools        Off Topic Tools     Off Topic     Contests Creator Corner     Graphics Design        Photoshop        GIMP     Basic Creator Help     Catalog And Product Showcase     3D Meshing        3Ds Max        Sketchup        Blender Gangsters with Connections     White Hat Activities        Google Hacking        Trackers Programming Corner     Coding        Python        .Net (C#, VB, etc)        Flash        JAVA        Autoit        Batch        HTML & CSS        Javascript        PHP        Other        IMVU Homepage Codes           General           About me Panel           Messages Panel           Special Someone Panel           Visitors Panel           New Products Panel           Rankings Panel           Wishlist Panel           My Badges Panel           Outfits Panel           Url Panel           Groups Panel           Slideshow Panel           My Room Panel           Sandbox panel           Layouts     Help & Requests Free Credits     Approved Methods     Submit Methods Free Money     Approved Methods     Submit Methods Adult Corner     Get Mafia AP Here     AP Lounge        AP Social Games        Casual Dating Tips     IMVU Slave Market & Escorts