Skip to content

Instantly share code, notes, and snippets.

@croepha
Created July 21, 2018 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save croepha/92c49346e2e11d2b6a85976d5a893b33 to your computer and use it in GitHub Desktop.
Save croepha/92c49346e2e11d2b6a85976d5a893b33 to your computer and use it in GitHub Desktop.
using System;
using System.ServiceProcess;
using System.Net.Sockets;
using System.Net;
using System.Text;
public partial class MyService : ServiceBase
{
UdpClient udp_client;
public MyService()
{
//InitializeComponent();
}
void UDPCallback(IAsyncResult ar) {
udp_client.BeginReceive(UDPCallback, null);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udp_client.Receive(ref ep);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("Received: {0} {1}", receiveString, ep.Address);
}
protected override void OnStart(string[] args)
{
Console.WriteLine("starting...");
udp_client = new UdpClient(3434);
udp_client.BeginReceive(UDPCallback, null);
}
protected override void OnStop()
{
Console.WriteLine("stopping....");
udp_client = null;
}
static void Main()
{
System.ServiceProcess.ServiceBase.Run(new MyService());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment