create a second plane listener that runs as a windows service

0

I currently need to create a listener that broadcasts over the network, and that listens to messages coming from any IP address on the network (it's a private network). I currently have this functionality implemented on a website through

HostingEnvironment.QueueBackgroundWorkItem (token => IniciaBroadCast (token));

My intention is to transfer this functionality to a windows service, which when receiving data, is able to store this data in a table in the database installed on the server.

This I need, to do this way, since nowadays, if the website goes down, the systems that send messages through UDP, can not send their messages.

here I have a bit of confusion, because I do not understand what I have to specify in my windows service, which I already have created, but it does not do anything .. since in the OnStart () method, I add the following code

        int listenPort = 15000;
        int TiempoEsperaEscucha = 10000;
        timerUp.Interval = TiempoEsperaEscucha;
        StartListener(listenPort);

but when I start the service, I get a message saying that the service started and ended immediately.

I add the complete code here, so that the problem is more understandable.

the service is called IsAlive () here the builder

public IsAlive()
    {
        InitializeComponent();
        timerUp = new System.Timers.Timer();
        int TiempoEsperaEscucha = 10000;
        timerUp.Interval = TiempoEsperaEscucha;
        timerUp.Elapsed += new  System.Timers.ElapsedEventHandler(Timmer_Elapsed);
    }

then in the OnStart () method

        protected override void OnStart(string[] args)
    {
        AccuroLog.WriteEntry("Iniciando servicio IsAlive");
        timerUp.Start();

    }

and finally, when starting the timmer

    private void Timmer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Logger.GrabaLog("Inicia Timmer_Elapsed", ArchivoTrace);

        int listenPort = 15000;
        IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);

        UdpClient client = new UdpClient(listenPort);
        UdpState state = new UdpState(client, remoteSender);

        client.BeginReceive(new AsyncCallback(DataReceived), state);

        // espera hasta que se pulse una tecla para salir
        //while (1 == 1)
        //{ }
        //Console.ReadKey();
        //client.Close();
        Logger.GrabaLog("termina Timmer_Elapsed", ArchivoTrace);


    }

here my problem is that this event of Timmer_Elapsed, lasts only a few moments, and is cut with Client.Close () .. then if a message arrives I will never receive it.

My question is how can I keep the port listening, without having to close the process ...

    
asked by Luis Gabriel Fabres 19.10.2017 в 23:00
source

1 answer

0

Good morning, I give you an example that I use to specify the service. I hope and serve you.

public class Service1
{
    Timer tmServicio = null;

    public Service1()
    {
        tmServicio = new Timer();
        tmServicio.Interval = 300000;
        tmServicio.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
    }

    protected override void OnStart(string[] args)
    {
        tmServicio.Start();
    }

    protected override void OnStop()
    {
        tmServicio.Stop();
    }

    // Se ejecuta cada cierto tiempo establecido en el timer
     private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //Aqui van las funciones que quieres que realize el servicio de windows
    }
}

Greetings.

    
answered by 20.10.2017 в 00:55