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 ...