I've been experimenting the UDP sending and receiving in C# and have a strange issue. The code works fine in a console app, but the client.Receive method is blocked when I try to use the exact same code in a Service. The Service runs normally and doesn't abort, and I have logging writing to a text file, so I know it gets to the Receive. Anyone have any ideas? Code below...
public partial class Service1 : ServiceBase
{
private bool serviceStarted = false;
Thread listenerThread;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
serviceStarted = false;
WriteLog("UDPListener Service Starting");
ThreadStart thread = new ThreadStart(StartListening);
listenerThread = new Thread(thread);
serviceStarted = true;
listenerThread.Start();
}
protected override void OnStop()
{
WriteLog("UDPListener Service Stopping");
serviceStarted = false;
listenerThread.Join(new TimeSpan(0, 0, 5));
}
private void StartListening()
{
WriteLog("Worker thread spawned.");
UdpClient client = new UdpClient(40000);
while (serviceStarted)
{
WriteLog("Service is started. Getting endpoint.");
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 40000);
WriteLog("Thread is listening...");
byte[] content = client.Receive(ref remoteIPEndPoint);
WriteLog("Receive unblocked.");
if (content.Length > 0)
{
string message = Encoding.ASCII.GetString(content);
WriteLog("UDPListener Message = " + message);
}
}
Thread.CurrentThread.Abort();
}
private void WriteLog(string strMessage)
{
FileStream filestream = new FileStream(@"c:\temp\UDPClientLog.txt",
FileMode.OpenOrCreate,
FileAccess.Write);
StreamWriter streamwriter = new StreamWriter(filestream);
streamwriter.BaseStream.Seek(0, SeekOrigin.End);
streamwriter.WriteLine(DateTime.Now.ToLongDateString() +
" at " +
DateTime.Now.ToLongTimeString() +
": " +
strMessage +
"\n");
streamwriter.Flush();
streamwriter.Close();
}
}