'programming'에 해당되는 글 387건

  1. 2011.04.20 c# windows service tcp socket server

반응형

http://www.c-sharpcorner.com/Forums/Thread/59726/


c# windows service tcp socket server  
 
  Posted on: 27 May 2009          
 
 
 
Hi,

I think this is the closest thread I could find, I have been going crazy trying to find what is wrong with my code. I am writing a Windows Service which is a TCP server that executes Power Shell scripts and send the result back to clients. I call the service by sending a simple string as paramater like: perl client.pl get-command . The server returns the results and my perl client receives and displays them. Here is the code, and I will be very thankful for any tips and pointers:

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public TcpListener threadListener;

        public Service1()
        {
            InitializeComponent();
        }

        TcpListener server = null;
        Thread serverThread = null;
        bool serverPaused = false;

        protected override void OnStart(string[] args)
        {
            serverThread = new Thread(new ThreadStart(Serve));
            serverThread.Start();
        }

        protected override void OnStop()
        {
        }

        private void Serve()
        {
            server = new TcpListener(IPAddress.Any, 8888);
            server.Start();           
            while (server != null)
            {
                try
                {
                    if (serverPaused) { Thread.Sleep(10); continue; }
                    Socket s = server.AcceptSocket();
                    EventLog.WriteEntry("Accepted " + ((IPEndPoint)s.RemoteEndPoint));
                  
                    string welcome = "Message from PowerShell Server ...";
                    byte[] b = System.Text.Encoding.ASCII.GetBytes(welcome);
                    s.Send(b, b.Length, 0);
                    EventLog.WriteEntry("Sent welcome to " + ((IPEndPoint)s.RemoteEndPoint));                 
                                       
                    //PS Server Code
                    byte[] data = new byte[204800];
                    int recv;
                    TcpClient client = threadListener.AcceptTcpClient();
                    NetworkStream ns = client.GetStream();
                    recv = ns.Read(data, 0, data.Length);                   
                    string cmd = Encoding.ASCII.GetString(data, 0, recv);          
                    string stringRunShell = RunShell(cmd);
                    byte[] byteData = Encoding.ASCII.GetBytes(stringRunShell.ToString());
                    // Begin sending the data to the remote machine.
                    if (byteData.Length > 0)
                    {
                        ns.Write(byteData, 0, byteData.Length);
                        ns.Close();
                    }
                    else
                    {
                        ns.Write(data, 0, recv);
                    }                 
                    s.Close();
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }

            }
        }

        public string RunShell(string cmd)
        {                       
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(cmd);
            //Conver the objects returned to formatted strings
            pipeline.Commands.Add("Out-String");
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();
            StringBuilder builtString = new StringBuilder();
            foreach (PSObject obj in results)
            {
                builtString.AppendLine(obj.ToString());
            }
            //Console.WriteLine(stringBuilder.ToString());
            return builtString.ToString();
        }  
    }
}

Thanks
-Sami

 
 

반응형
Posted by 공간사랑
,