简体   繁体   English

客户端如何与具有多个适配器的计算机上的Windows服务通信?

[英]How does a client communicate with a Windows Service on a machine with multiple adapters?

I've inherited a Windows Service + Client application written in C#. 我继承了用C#编写的Windows Service + Client应用程序。 The Windows Service opens a TCP/IP socket for listening as follows: Windows服务打开一个TCP / IP套接字以进行监听,如下所示:

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry iphe = Dns.Resolve(Dns.GetHostName());
socket.Bind(new IPEndPoint(iphe.AddressList[0], ExportServiceRequest.DefaultPort));
socket.Listen(2);
// Wait for incoming connections and Accept them.

while the client connects as follows: 而客户端连接如下:

using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
    IPHostEntry iphe = Dns.Resolve(Dns.GetHostName());
    IPEndPoint ep = new IPEndPoint(iphe.AddressList[0], ExportServiceRequest.DefaultPort);
    socket.Connect(ep);
    // Talk to the server
}

The problem is that on certain machines with multiple network adapters, the 'wrong' adapter is picked by Dns.Resolve(), and the client connection fails. 问题是,在具有多个网络适配器的某些计算机上,Dns.Resolve()选择了“错误”的适配器,并且客户端连接失败。

I am aware that this code is aged and reeking, and haven't coded much raw socket code. 我知道这段代码已经老化并且日渐流行,并且没有编写太多原始套接字代码。 Is there a best way to implement a Windows Service listening on a socket, such that at least local connections (from within the same machine) always succeed, regardless of how many network adapters the machine has? 是否有最佳方法来实现在套接字上侦听Windows服务,以使至少本地连接(从同一台计算机中)总是成功,而不管该计算机有多少个网络适配器?

Edit: my question appears poorly formulated. 编辑:我的问题似乎措辞不好。 At the end of the day, I want the Windows Service to be accessible by a client, which always is running on the same machine, with no need for configuration. 最终,我希望客户端可以访问Windows服务,该客户端始终在同一台计算机上运行,​​而无需进行配置。 To anthropomorphize, I want the Windows Client to just yell at the server, "what IP address can I talk to you, oh Service running on TCP port ExportServiceRequest.DefaultPort on $LOCALHOST$? I want to talk to you", and have the ensuing TCP/IP conversation just work. 为了拟人化,我希望Windows客户端对服务器大吼大叫,“我可以与您交谈哪个IP地址,哦$ LOCALHOST $上的TCP端口ExportServiceRequest.DefaultPort上运行的服务?我想与您交谈”确保TCP / IP对话正常工作。

Socket.Bind : Socket.Bind

Before calling Bind , you must first create the local IPEndPoint from which you intend to communicate data. 在调用Bind之前,您必须首先创建要用来与之通信数据的本地IPEndPoint If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. 如果您不关心分配了哪个本地地址,则可以使用IPAddress.Any作为地址参数来创建IPEndPoint ,基础服务提供商将分配最合适的网络地址。 This might help simplify your application if you have multiple network interfaces. 如果您有多个网络接口,这可能有助于简化您的应用程序。

(emphasis added). (强调)。 So that would be one suggested change. 因此,这将是一项建议的更改。

I'd also suggest switching your Connect call to Connect(string,int) : 我还建议将您的Connect调用切换为Connect(string,int)

Establishes a connection to a remote host. 建立与远程主机的连接。 The host is specified by a host name and a port number. 主机由主机名和端口号指定。

(a host value of 'localhost' should be sufficient there) (在那里, host'localhost'就足够了)

That is, get rid of all of this mucking about with DNS, etc, and just rely on the underlying infrastructure to resolve these issues. 也就是说,摆脱所有与DNS等相关的问题,仅依靠底层基础结构来解决这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM