Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace ServerSocket
- {
- class Program
- {
- public static string GetLocalIPAddress()
- {
- var host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (var ip in host.AddressList)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- return ip.ToString();
- }
- }
- throw new Exception("No network adapters with an IPv4 address in the system!");
- }
- static void Main(string[] args)
- {
- try
- {
- IPAddress IP = IPAddress.Parse(GetLocalIPAddress());
- int PORT = 8888;
- string FILE_EXEC = "exec.bat";
- for (int i = 0; i < args.Length; i++)
- {
- string[] arg = args[i].Split(':');
- string a = arg[0].Replace("\"", "").Trim();
- string b = arg[1].Replace("\"", "").Trim();
- switch (a)
- {
- case "-p":
- int.TryParse(b, out PORT);
- break;
- case "-f":
- FILE_EXEC = b;
- break;
- default:
- break;
- }
- }
- TcpListener myList = new TcpListener(IP, PORT);
- /* Start Listeneting at the specified port */
- myList.Start();
- Console.WriteLine("The server is running at port {0}...",PORT);
- Console.WriteLine("The local End point is :" +
- myList.LocalEndpoint);
- Console.WriteLine("Waiting for a connection.....");
- while (true)
- {
- Socket s = myList.AcceptSocket();
- string RemoteEndPoint = ""+s.RemoteEndPoint;
- Console.WriteLine("[{0}] is Connected..", RemoteEndPoint);
- byte[] b = new byte[100];
- int k = s.Receive(b);
- string receive = "";
- for (int i = 0; i < k; i++)
- receive += Convert.ToChar(b[i]);
- Console.WriteLine("[{0}] Has Sent: {1}", RemoteEndPoint, receive);
- System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo(FILE_EXEC);
- ProcStartInfo.RedirectStandardOutput = true;
- ProcStartInfo.UseShellExecute = false;
- ProcStartInfo.CreateNoWindow = false;
- ProcStartInfo.RedirectStandardError = true;
- System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
- ProcStartInfo.Arguments = "ServerSocket";
- MyProcess.StartInfo = ProcStartInfo;
- MyProcess.Start();
- string output = MyProcess.StandardOutput.ReadToEnd();
- MyProcess.WaitForExit();
- ASCIIEncoding asen = new ASCIIEncoding();
- s.Send(asen.GetBytes(output));
- s.Close();
- Console.WriteLine("[{0}] Connection Closed", RemoteEndPoint);
- }
- /* clean up */
- //myList.Stop();
- }
- catch (Exception e)
- {
- Console.WriteLine("Error..... " + e.StackTrace);
- Environment.Exit(1);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment