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.