Created
July 21, 2018 13:03
-
-
Save croepha/92c49346e2e11d2b6a85976d5a893b33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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