Interact with Windows service to send and receive data?

Asked

Viewed 384 times

1

I created a service on Windows 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 make this query on another IP and return the result to the application.

Within the Onstart method I created, a Streamwriter that fills a txt, to make sure that the service is working.

How do I send requests to this service and receive your answers?

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();
        }
    }
}
  • 1

    Is your service open to port and listening? I advise using IP 127.0.0.1, this is localhost.

  • This is the Onstart method of the service, when it is started it makes some notes in text file, need to know how from the web application and can call this method or any other that is created within the service, understand?

  • Start by improving your code. Enter error checks and exception handling. Recording all errors in file as it is a service without interface.

  • I even intend to do this, when I can make the calls from the application, at the moment it doesn’t help much to have a written service perfectly if I can’t, yet, communicate with it.

  • @Isalamon If you have any suggestion of material or link to another question that helps me to make this communication, I thank you in advance...

  • without knowing what error is occurring there is no way to help you. On the Internet you find several examples client/server.

Show 1 more comment

1 answer

0


  • @A.Carvalho Thank you so much for really contributing so that I find the answer to my question... These links are practical examples of what I will definitely use...

  • 1

    Can you bring here more information that is found in these links? If the links no longer work your answer is useless...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.