Interact with Windows service to send and receive data?

1

I created a service in Windows that is installed on localhost and now has to interact with this service.

Within my web application I will give a query command that will be sent to this service and this service will do this query on another IP and return the result to the application.

Within the OnStart method I've created, a StreamWriter that fills a txt, to make sure the service is working.

How do I submit requests for this service and receive your responses?

This is the Service code:

namespace ServWin
{
    public partial class Service1 : ServiceBase
    {
        StreamWriter arquivoLog;
        public Service1()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {

            arquivoLog = new StreamWriter(@"C:\Users\JoãoKleber\Desktop\teste.txt", true);
            arquivoLog.WriteLine("Teste de inclusão de Texto, na inicialização do Serviços " + DateTime.Now);

            string ordem;
            string host;
            int port;
            host = "192.168.0.103";
            port = 5000;
            ordem = "outputs";


            IPAddress[] IPs = Dns.GetHostAddresses(host);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(IPs[0], port);

            byte[] buffer = new byte[60];
            s.Receive(buffer);

            byte[] envDados = System.Text.Encoding.ASCII.GetBytes(ordem + "\n");
            s.Send(envDados);

            byte[] ret = new byte[29];
            s.Receive(ret);

            var tex = System.Text.Encoding.ASCII.GetString(ret, 4, 4).Replace("211", "");

            var vl = Convert.ToString(Convert.ToInt32(tex, 16), 2);
            vl = vl.PadLeft(8, '0');
            vl.ToCharArray();
            if (Convert.ToString(vl[7]) == "1") { arquivoLog.WriteLine("Relé 1 Acesso"); } else { arquivoLog.WriteLine("Relé 1 Apagado"); }
            if (Convert.ToString(vl[6]) == "1") { arquivoLog.WriteLine("Relé 2 Acesso"); } else { arquivoLog.WriteLine("Relé 2 Apagado"); }
            if (Convert.ToString(vl[5]) == "1") { arquivoLog.WriteLine("Relé 3 Acesso"); } else { arquivoLog.WriteLine("Relé 3 Apagado"); }
            if (Convert.ToString(vl[4]) == "1") { arquivoLog.WriteLine("Relé 4 Acesso"); } else { arquivoLog.WriteLine("Relé 4 Apagado"); }
            if (Convert.ToString(vl[3]) == "1") { arquivoLog.WriteLine("Relé 5 Acesso"); } else { arquivoLog.WriteLine("Relé 5 Apagado"); }
            if (Convert.ToString(vl[2]) == "1") { arquivoLog.WriteLine("Relé 6 Acesso"); } else { arquivoLog.WriteLine("Relé 6 Apagado"); }
            if (Convert.ToString(vl[1]) == "1") { arquivoLog.WriteLine("Relé 7 Acesso"); } else { arquivoLog.WriteLine("Relé 7 Apagado"); }
            if (Convert.ToString(vl[0]) == "1") { arquivoLog.WriteLine("Relé 8 Acesso"); } else { arquivoLog.WriteLine("Relé 8 Apagado"); }

            arquivoLog.Flush();

        }

        protected override void OnStop()
        {
            arquivoLog.WriteLine("Teste de Inclusão na finalização do Serviço " + DateTime.Now);
            arquivoLog.Close();
        }
    }
}
    
asked by anonymous 18.05.2016 / 16:15

1 answer

0

Some time ago I created a windows service, this service opened a socket to exchange information with a client application.

Some links that can help you regarding the server and client model:

link link

    
22.05.2016 / 23:42