1

I have a Socket program in my Windows Service application to monitor particular Socket for reading and writing data into text file.I need my application to establish the connection and continuously read/write the data from the machine as soon as new data enters into the machine. Now when i am trying to do the above explained task in debug mode of my visual studio it is happening perfectly but as soon as i try to run it from services.msc it is neither dumping(writing) any data into text file from the machine..

I am using Visual Studio Setup project to create and install the Setup...

Now here is my Applications code..

This is my main program code..

  static void Main()
    {

if DEBUG

        Service1 myservice = new Service1();
        myservice.OnDebug();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

else

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };
        ServiceBase.Run(ServicesToRun);

endif

    }

and this is my Socket logic codes..

    string ipaddress, textfileSaveLocation;
    Byte[] bBuf;
    string buf;
    private System.Threading.Thread _thread;
    private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);

    string df = "";

    public Service1()
    {
        InitializeComponent();
    }

    public void OnDebug()
    {

        OnStart(null);

    }


    protected override void OnStart(string[] args)
    {
        _thread = new Thread(DoWork);
        _thread.Start();

    }


    private void DoWork()
    {
        // create and monitor socket here...

        ipaddress = "";
        int port = int.Parse("");
        textfileSaveLocation = "";

        byte[] data = new byte[1024];
        string stringData;


        IPAddress ipadd = IPAddress.Parse(ipaddress);
        IPEndPoint ipend = new IPEndPoint(ipadd, port);
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sock.NoDelay = false;
        try
        {
            sock.Connect(ipend);

        }
        catch (Exception dfg)
        {

            return;
        }
        try
        {
            buf = String.Format("Client Here");
            bBuf = Encoding.ASCII.GetBytes(buf);
            sock.Send(bBuf);


            while (!_shutdownEvent.WaitOne(0))
            {
                data = new byte[1024];
                int recv = sock.Receive(data);
                stringData = Encoding.ASCII.GetString(data, 0, recv);

                try
                {
                    FileStream cd = new FileStream(textfileSaveLocation, FileMode.OpenOrCreate);
                    StreamWriter cdf = new StreamWriter(cd);
                    cdf.WriteLine(df);
                    cdf.WriteLine(stringData);
                    cdf.Dispose();
                    cd.Dispose();
                }
                catch (Exception abcd)
                {
                }


            }
            sock.Shutdown(SocketShutdown.Both);
            sock.Close();
        }
        catch (Exception DFGFD)
        {
        }


    }


    protected override void OnStop()
    {
        _shutdownEvent.Set();
        _thread.Join();  // wait for thread to stop
    }
}

I have build the application under the release mode and used that to create the Visual Studio Setup project file to install the application as Windows Service.. But i am not getting where i am missing the logic or flow..

Also I am trying to install this service through remote login by using Team Viewer Access.

Adi
  • 1,395
  • 11
  • 37
  • 61
  • 1
    It may be that the user account under which the service runs lacks the permissions to write on the desired file system location. – Mauro Cerutti Jan 17 '14 at 12:13
  • 1
    Where is it going wrong? You should surely in this stage, log the exceptions. E.g. EventLog.WriteEntry(). You can also debug services with vs. You can find out how: http://stackoverflow.com/questions/4678819/how-to-debug-windows-services-in-visual-studio – Ceelie Jan 17 '14 at 21:13

0 Answers0