c#中的套接字程序在Visual Studio的调试模式下读取时未在Windows服务上读取
我的Windows服务应用程序中有一个套接字程序,用于监视特定的套接字,以便将数据读写到文本文件中。我需要我的应用程序建立连接,并在新数据进入机器后立即从机器中连续读/写数据。 现在,当我尝试在visual studio的调试模式下执行上面解释的任务时,它正在完美地执行,但只要我尝试从services.msc运行它,它就不会从机器将任何数据转储(写入)到文本文件中 我正在使用Visual Studio安装项目创建和安装安装程序 这是我的应用程序代码 这是我的主要程序代码c#中的套接字程序在Visual Studio的调试模式下读取时未在Windows服务上读取,c#,multithreading,visual-studio-2010,sockets,windows-services,C#,Multithreading,Visual Studio 2010,Sockets,Windows Services,我的Windows服务应用程序中有一个套接字程序,用于监视特定的套接字,以便将数据读写到文本文件中。我需要我的应用程序建立连接,并在新数据进入机器后立即从机器中连续读/写数据。 现在,当我尝试在visual studio的调试模式下执行上面解释的任务时,它正在完美地执行,但只要我尝试从services.msc运行它,它就不会从机器将任何数据转储(写入)到文本文件中 我正在使用Visual Studio安装项目创建和安装安装程序 这是我的应用程序代码 这是我的主要程序代码 static voi
static void Main()
{
如果调试
其他的
恩迪夫
这是我的套接字逻辑代码
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
}
}
我已经在发布模式下构建了应用程序,并使用该模式创建Visual Studio安装项目文件,以将应用程序作为Windows服务安装。。
但我并没有错过逻辑或流程
另外,我正在尝试使用Team Viewer Access通过远程登录安装此服务。可能是运行此服务的用户帐户缺少在所需文件系统位置写入的权限。哪里出了问题?在这个阶段中,您肯定应该记录异常。例如EventLog.WriteEntry()。您还可以使用vs调试服务。您可以了解如何:
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
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
}
}