![](/img/trans.png)
[英]TCP/IP Socket is not reading data from machine when running as Windows Service in c#
[英]Any difference opening a TCP/IP port when running code as an application vs as a service?
我正在創建一個將打開偵聽TCP / IP端口(使用稱為DNP的協議)的服務。
為了調試程序,我一直在Program.cs文件中使用以下代碼:
static void Main()
{
#if(!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServerDNP()
};
ServiceBase.Run(ServicesToRun);
#else
ServerDNP ServerDNPProcess = new ServerDNP();
ServerDNPProcess.MainProcess();
#endif
}
我的OnStart看起來像這樣:
protected override void OnStart(string[] args)
{
this.workerThread = new Thread(new ThreadStart(this.MainProcess));
this.keepRunning = true;
this.workerThread.Start();
}
在實際過程中,我向日志文件輸出了很多信息,無論我是否將其作為DEBUG運行或將輸出發布到該文件看起來都一樣。
當我以DEBUG身份運行它時,TCP / IP端口完全沒有問題,我可以從遠程計算機與之通信。 但是,當我將其作為服務運行時,我在遠程計算機上始終收到“套接字無活動”錯誤。
為了使服務打開TCP / IP端口,我需要對服務做些特殊的事情嗎? 我一直在搜索和玩耍一段時間,無法解決任何問題。
我只是個白痴
我沒有意識到我需要為該過程打開防火牆,而不僅僅是在我將其作為應用程序運行時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.