如何获取.NET分配的IP地址和端口号
我有两个 Windows 窗体应用程序,一个充当服务器(即 Winform:服务器角色),另一个充当客户端(即 Winform: 客户端角色)。在我的 LAN 设置中,有 6 台 PC,这些 PC 通过 8 端口交换机相互连接,并且每台 PC 都有多个 LAN 卡。
有一台 PC 运行 [Winform:服务器角色],另外五台 PC 运行 [Winform:客户端角色]。在[Winform:服务器角色]中,我使用以下代码来获取本地 IP地址和端口号,[Winform:服务器角色]将根据此自动分配的IP地址和端口号侦听所有传入的TCP请求。
Dim Listener As System.Net.Sockets.TcpListener
Dim Client As New System.Net.Sockets.TcpClient
Dim Message As String = ""
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 0)
Listener.Start()
End Sub
所有[Winform:客户端角色]如何在运行时知道我的[Winform:服务器角色] IP 地址和端口号?
我需要澄清我的意图。我目前的意图可能不正确。我尝试创建一个“零配置客户端-服务器网络”,即即插即用。玩。服务器将知道客户端在哪里,反之亦然。我知道有一个程序(即 MaxiVista)已经做到了这一点。
MaxiVista有两个应用程序,即服务器和客户端。用户只需在指定为服务器角色的PC上执行服务器应用程序,并在另一台指定为客户端角色的PC上执行客户端应用程序。然后服务器将能够找到同一 LAN 中的所有正在执行的客户端。
我的意图仅此而已。在同一 LAN 内即插即用“零配置客户端-服务器网络”。
I have two Windows Forms applications, one acts as server (that is, Winform:Server role) and another one as a client (that is, Winform: Client role). In my LAN setup, there are 6 PCs and these PCs connected to each other via a 8-port switch and each PC has more than one LAN card.
There is one PC running [Winform: Server role] and five others running the [Winform: client role]. In [Winform: Server role], I'm using following code to obtain the local IP address and port number and the [Winform: Server role] will listen to all incoming TCP requests according to this auto-assigned IP address and port number.
Dim Listener As System.Net.Sockets.TcpListener
Dim Client As New System.Net.Sockets.TcpClient
Dim Message As String = ""
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 0)
Listener.Start()
End Sub
How do all [Winform: Client role] know my [Winform: Server role] IP address and port number at run time?
I need to clarify my intention. My current approach to my intention maybe incorrect. I attempt to create a 'zero configuration client-server networking' and that is plug & play. The server will know where the client and vise versa. I know there is a program (that is, MaxiVista) has done that exactly.
MaxiVista has two applications, that is, server and client. Users only need to execute the server application in PC designated as server role and execute the client application in another PC designated as client role. Then the server will be able to find all executing clients in the same LAN.
My intention is just that. Plug and play 'zero configuration client-server networking' within the same LAN.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,他们真的不知道。
您可以配置 DNS,例如
yourappserver
指向您的服务器,然后让客户端连接到该服务器,但这显然有点复杂(加上对值进行硬编码并不是一个好方法这)。您可以使用某种服务公告 - 例如通过 mDNS。这是通过让服务器定期宣布“我是一个小服务器,短而粗壮(端口 12345 上的WhateverYourAppIsCalled 的服务器)”和您的客户端来监听此类请求,甚至请求它们(“周围是否有WhateverYourAppIsCalled 的服务器”这里?”)。另请参阅:http://en.wikipedia.org/wiki/Zero_configuration_networking#Service_discovery
(在紧要关头,您可以让服务器向网络广播其存在并让客户端侦听此类广播,但随后您基本上是在重新实现 mDNS)
Well, they don't, really.
You could configure DNS for e.g.
yourappserver
to point to your server and then have the clients connect to that, but that is obviously a bit complicated (plus hard-coding the value isn't a great way to do this).What you could use is some sort of service announcement - e.g. via mDNS. This works by having the server periodically announce "I'm a little server, short and stout (server of WhateverYourAppIsCalled on port 12345)" and your clients to listen for such requests, or even requesting them ("is there a server of WhateverYourAppIsCalled around here?"). See also this: http://en.wikipedia.org/wiki/Zero_configuration_networking#Service_discovery
(In a pinch, you could make the server broadcast its presence to the network and have the clients listen for such broadcasts, but then you're basically re-implementing mDNS)
客户端无法自动找出服务器的 IP 地址。以下是解决问题的一些选择:
There is no way for the clients to automatically figure out the IP address of the server. Here are some choices for solving your problem:
从众所周知的机器名称到 IP 地址的映射是由命名服务完成的,DNS 是标准服务。如果您无法使 TcpClient.Connect(string, int) 工作,请与您的 LAN 管理员联系。
您不能让服务器像这样开始侦听端口 0。它必须是众所周知的端口号,否则客户端将不知道在 Connect() 调用中使用什么端口号。选择一个数字,任何数字,一千以上。像 1667 一样。如果与该计算机上使用相同端口号运行的另一个 TCP 服务器发生冲突,那么您就会发现。在客户端上,服务器名称和端口号属于一个设置,以便 LAN 管理员可以轻松更改。
Mapping from a well-known machine name to an IP address is done by a naming service, DNS is the standard one. Talk to your LAN administrator if you cannot get TcpClient.Connect(string, int) working.
You cannot afford to have the server start listening on port 0 like that. It must be a well-known port number or the clients will have no idea what port number to use in their Connect() call. Pick a number, any number, above a thousand. Like 1667. If there's a conflict with another TCP server running on that machine using the same port number then you'll find out. On the clients, the server name and port number belongs in a setting so that it can be easily changed by the LAN administrator.