WebSocket windows 服务侦听端口 8080
WebSocket windows service listening on port 8080
我是 Windows 服务开发的新手,需要用 C# 构建一个服务,它会在端口 8080
上侦听传入的数据,然后对其进行解析。我找到了有关 System.Net.WebSockets
、System.Net.Sockets
和第三方库(例如 SuperSocket
)的信息。
有这个 example, but not sure what goes in the OnStart()
and what goes in OnStop()
methods of my Windows service class. Another example is here, but that's also not covering Windows service specifically. For basic Windows service development, there's this MSDN article.
我想这会进入 OnStart()
:
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
serverSocket.Listen(128);
serverSocket.BeginAccept(null, 0, OnAccept, null);
我会在 OnStop()
中输入什么?
传入的数据流不需要任何身份验证。我还需要握手吗?
感谢您的帮助。
由于您希望使用端口 8080 作为侦听端口,我假设您实际上是在处理 http 流量。您应该研究 OWIN Self Hosted web server. Alternatively NancyFx 也可以工作,而不是使用原始套接字。
OnStop()
在 windows 尝试停止服务时调用。您可以将其视为您的 Dispose 服务。 OnStart()
同样是你做初始化的函数。我建议您查看非常好的库 TopShelf 以开始编写 Windows 服务。
我是 Windows 服务开发的新手,需要用 C# 构建一个服务,它会在端口 8080
上侦听传入的数据,然后对其进行解析。我找到了有关 System.Net.WebSockets
、System.Net.Sockets
和第三方库(例如 SuperSocket
)的信息。
有这个 example, but not sure what goes in the OnStart()
and what goes in OnStop()
methods of my Windows service class. Another example is here, but that's also not covering Windows service specifically. For basic Windows service development, there's this MSDN article.
我想这会进入 OnStart()
:
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
serverSocket.Listen(128);
serverSocket.BeginAccept(null, 0, OnAccept, null);
我会在 OnStop()
中输入什么?
传入的数据流不需要任何身份验证。我还需要握手吗?
感谢您的帮助。
由于您希望使用端口 8080 作为侦听端口,我假设您实际上是在处理 http 流量。您应该研究 OWIN Self Hosted web server. Alternatively NancyFx 也可以工作,而不是使用原始套接字。
OnStop()
在 windows 尝试停止服务时调用。您可以将其视为您的 Dispose 服务。 OnStart()
同样是你做初始化的函数。我建议您查看非常好的库 TopShelf 以开始编写 Windows 服务。