Guest User

Untitled

a guest
Jul 13th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ServerSocket
  10. {
  11.     class Program
  12.     {
  13.         public static string GetLocalIPAddress()
  14.         {
  15.             var host = Dns.GetHostEntry(Dns.GetHostName());
  16.             foreach (var ip in host.AddressList)
  17.             {
  18.                 if (ip.AddressFamily == AddressFamily.InterNetwork)
  19.                 {
  20.                     return ip.ToString();
  21.                 }
  22.             }
  23.             throw new Exception("No network adapters with an IPv4 address in the system!");
  24.         }
  25.         static void Main(string[] args)
  26.         {
  27.             try
  28.             {
  29.                 IPAddress IP = IPAddress.Parse(GetLocalIPAddress());
  30.                 int PORT = 8888;
  31.                 string FILE_EXEC = "exec.bat";
  32.  
  33.                 for (int i = 0; i < args.Length; i++)
  34.                 {
  35.                     string[] arg = args[i].Split(':');
  36.                     string a = arg[0].Replace("\"", "").Trim();
  37.                     string b = arg[1].Replace("\"", "").Trim();
  38.                     switch (a)
  39.                     {
  40.                         case "-p":
  41.                             int.TryParse(b, out PORT);
  42.                             break;
  43.                         case "-f":
  44.                             FILE_EXEC = b;
  45.                             break;
  46.                         default:
  47.                             break;
  48.                     }
  49.                 }
  50.                
  51.  
  52.  
  53.                 TcpListener myList = new TcpListener(IP, PORT);
  54.  
  55.                 /* Start Listeneting at the specified port */
  56.                 myList.Start();
  57.  
  58.                 Console.WriteLine("The server is running at port {0}...",PORT);
  59.                 Console.WriteLine("The local End point is  :" +
  60.                                   myList.LocalEndpoint);
  61.                 Console.WriteLine("Waiting for a connection.....");
  62.  
  63.  
  64.                 while (true)
  65.                 {
  66.                     Socket s = myList.AcceptSocket();
  67.                     string RemoteEndPoint = ""+s.RemoteEndPoint;
  68.                     Console.WriteLine("[{0}] is Connected..", RemoteEndPoint);
  69.                     byte[] b = new byte[100];
  70.                     int k = s.Receive(b);
  71.                     string receive = "";
  72.                     for (int i = 0; i < k; i++)
  73.                         receive += Convert.ToChar(b[i]);
  74.                     Console.WriteLine("[{0}] Has Sent: {1}", RemoteEndPoint, receive);
  75.  
  76.                     System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo(FILE_EXEC);
  77.                     ProcStartInfo.RedirectStandardOutput = true;
  78.                     ProcStartInfo.UseShellExecute = false;
  79.                     ProcStartInfo.CreateNoWindow = false;
  80.                     ProcStartInfo.RedirectStandardError = true;
  81.                     System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
  82.                     ProcStartInfo.Arguments = "ServerSocket";
  83.                     MyProcess.StartInfo = ProcStartInfo;
  84.                     MyProcess.Start();
  85.                     string output = MyProcess.StandardOutput.ReadToEnd();
  86.                     MyProcess.WaitForExit();
  87.                     ASCIIEncoding asen = new ASCIIEncoding();
  88.                     s.Send(asen.GetBytes(output));
  89.                     s.Close();
  90.                     Console.WriteLine("[{0}] Connection Closed", RemoteEndPoint);
  91.                 }
  92.                
  93.                 /* clean up */
  94.                
  95.                 //myList.Stop();
  96.  
  97.             }
  98.             catch (Exception e)
  99.             {
  100.                 Console.WriteLine("Error..... " + e.StackTrace);
  101.                 Environment.Exit(1);
  102.             }
  103.         }
  104.     }
  105. }
Add Comment
Please, Sign In to add comment