Click here to Skip to main content
15,919,028 members
Home / Discussions / C#
   

C#

 
GeneralRe: I got job in c# PinPopular
DaveyM6910-Oct-08 8:55
professionalDaveyM6910-Oct-08 8:55 
JokeRe: I got job in c# Pin
nelsonpaixao10-Oct-08 12:34
nelsonpaixao10-Oct-08 12:34 
QuestionVS SDK - Language Service Package Pin
Charith Jayasundara10-Oct-08 5:58
Charith Jayasundara10-Oct-08 5:58 
AnswerRe: VS SDK - Language Service Package Pin
leppie10-Oct-08 6:08
leppie10-Oct-08 6:08 
GeneralRe: VS SDK - Language Service Package Pin
Charith Jayasundara10-Oct-08 7:03
Charith Jayasundara10-Oct-08 7:03 
QuestionRe: VS SDK - Language Service Package Pin
led mike10-Oct-08 7:11
led mike10-Oct-08 7:11 
AnswerRe: VS SDK - Language Service Package Pin
leppie10-Oct-08 22:57
leppie10-Oct-08 22:57 
QuestionWindows Service Syslog for UDP port 514 Pin
Dimitris Tselios10-Oct-08 4:41
Dimitris Tselios10-Oct-08 4:41 
I try to create a Windows Service which listens to UDP port 514, i install it using installutil.exe and i get the following error message:

---------------------------
Services
---------------------------
The SyslogService2005 service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
---------------------------
OK
---------------------------

In practise it starts working till the progressbar in Properties Window ends!

my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
//using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net.Sockets;
using System.Net;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Threading;
using System.IO;

namespace SyslogService2005
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
            this.ServiceName = "SyslogServiceUDP514";
            this.CanStop = true;
            this.CanPauseAndContinue = true;
            this.AutoLog = true;
        }

        private const int listenPort = 514;

        class UdpState
        {
            public UdpClient u;
            public IPEndPoint e;
        }


        public static bool messageReceived = false;

        public static bool processWorking = false;

        public static void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;


            Byte[] receiveBytes = u.EndReceive(ar, ref e);
            string receiveString = Encoding.ASCII.GetString(receiveBytes);
            LogMessageToFile(receiveString);

            //Console.WriteLine("Received: {0}", receiveString);
            messageReceived = true;


        }



        private static void StartListener()
        {
            // Receive a message and write it to the console.
            //LogMessageToFile("ReceiveMessage");

            IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
            //LogMessageToFile("UdpClient");
            UdpClient u = new UdpClient(e);
            //LogMessageToFile("UdpState");
            UdpState s = new UdpState();
            //LogMessageToFile("s.e = e");
            s.e = e;
           // LogMessageToFile("s.u");
            s.u = u;


            //Console.WriteLine("listening for messages");
            
                //LogMessageToFile("Start Asychronous");
            while (processWorking==true)
            {
                u.BeginReceive(new AsyncCallback(ReceiveCallback), s);

                // Do some work while we wait for a message. For this example,
                // we'll just sleep
                while (!messageReceived)
                {
                    //LogMessageToFile("Sleep 100 ms");
                    Thread.Sleep(100);
                }
            }
            


        }


        static void LogMessageToFile(string msg)
        {
            System.IO.StreamWriter sw = System.IO.File.AppendText("C:\\Services\\LogFile.txt");
            try
            {
                string logLine = System.String.Format("{0:G}: {1}.", System.DateTime.Now, msg);
                sw.WriteLine("+----------------------------------------------------------------+");

                sw.WriteLine(logLine);
            }
            finally
            {
                sw.Close();
            }
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            //LogMessageToFile("Service Started");

            processWorking = true;

            //StartListener();
            //ThreadStart job = new ThreadStart(StartListener);
            //Thread thread = new Thread(job);
            //thread.Start();
            //LogMessageToFile("StartListener Started");
            StartListener();
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            if ((Thread.CurrentThread != null) && (Thread.CurrentThread.IsAlive))
            {
                processWorking = false;
                Thread.Sleep(5000);
                Thread.CurrentThread.Abort();

                LogMessageToFile("Service Stopped");

            }
        }
    }
}

AnswerRe: Windows Service Syslog for UDP port 514 Pin
led mike10-Oct-08 5:07
led mike10-Oct-08 5:07 
GeneralRe: Windows Service Syslog for UDP port 514 Pin
Dimitris Tselios10-Oct-08 5:34
Dimitris Tselios10-Oct-08 5:34 
GeneralRe: Windows Service Syslog for UDP port 514 Pin
led mike10-Oct-08 5:41
led mike10-Oct-08 5:41 
GeneralRe: Windows Service Syslog for UDP port 514 Pin
leppie10-Oct-08 6:18
leppie10-Oct-08 6:18 
GeneralRe: Windows Service Syslog for UDP port 514 Pin
Dimitris Tselios12-Oct-08 2:11
Dimitris Tselios12-Oct-08 2:11 
QuestionRe: Windows Service Syslog for UDP port 514 Pin
Mark Salsbery10-Oct-08 5:56
Mark Salsbery10-Oct-08 5:56 
QuestionShell Programming - Loading a file selected on list view component Pin
Gianpaolo Barci10-Oct-08 4:34
Gianpaolo Barci10-Oct-08 4:34 
AnswerRe: Shell Programming - Loading a file selected on list view component Pin
DaveyM6910-Oct-08 4:59
professionalDaveyM6910-Oct-08 4:59 
GeneralRe: Shell Programming - Loading a file selected on list view component [modified] Pin
Gianpaolo Barci10-Oct-08 5:07
Gianpaolo Barci10-Oct-08 5:07 
GeneralRe: Shell Programming - Loading a file selected on list view component Pin
Dave Kreskowiak10-Oct-08 5:22
mveDave Kreskowiak10-Oct-08 5:22 
AnswerRe: Shell Programming - MY CODE! Pin
Gianpaolo Barci10-Oct-08 6:12
Gianpaolo Barci10-Oct-08 6:12 
GeneralRe: Shell Programming - MY CODE! Pin
Dave Kreskowiak10-Oct-08 6:49
mveDave Kreskowiak10-Oct-08 6:49 
GeneralRe: Shell Programming - MY CODE! Pin
Gianpaolo Barci10-Oct-08 7:00
Gianpaolo Barci10-Oct-08 7:00 
GeneralRe: Shell Programming - MY CODE! Pin
DaveyM6910-Oct-08 7:45
professionalDaveyM6910-Oct-08 7:45 
GeneralRe: Shell Programming - MY CODE! Pin
Gianpaolo Barci10-Oct-08 7:56
Gianpaolo Barci10-Oct-08 7:56 
GeneralRe: Shell Programming - MY CODE! Pin
DaveyM6910-Oct-08 8:25
professionalDaveyM6910-Oct-08 8:25 
GeneralRe: Shell Programming - MY CODE! Pin
Dave Kreskowiak10-Oct-08 8:27
mveDave Kreskowiak10-Oct-08 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.