如何指定开始监听哪个适配器?

我有这恰好具有2个网络适配器上不同的子网运行(一个网络商业局域网基础设施,一个用于TCP摄像机)

的PC上运行的应用程序

我有打开TCP服务器和特定的端口上侦听来自客户端的局域网上进来的连接的一个类。

的问题是,我的TCP服务器类初始化并开始连接到摄像机适配器上侦听。从商业LAN进来任何连接请求失败,则没有处理。

现在的问题是,如何指定开始监听哪个适配器?

代码摘录以下(这不是完整的类,只是关键方法)

Public Sub New(ByVal Name As String)
            'get config
            _bootStrap = New TCPServerBootstrap(Name)
            'start log file
            _Trace = New ACS.Utility.Logging("Connectivity." & Name & ".TcpServer." & _bootStrap.Port)
            _Trace.WriteLog("TCP Server Starting")
            Dim LocalIP As System.Net.IPAddress = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0)
            _myListener = New TcpListener(LocalIP, _bootStrap.Port)
            _Timer = New System.Timers.Timer
            _Timer.Interval = 500
            _Timer.Enabled = False
            _name = Name
            _details = _myListener.LocalEndpoint.ToString
        End Sub

    Public Sub BeginListening()
            'Starts the listener and uses the Asynchronous 'Begin' method to handle inbound connection attempts
            _Trace.WriteLog("Begin Listening on: " & _myListener.LocalEndpoint.ToString)
            _myListener.Start()
            _myListener.BeginAcceptSocket(New AsyncCallback(AddressOf HandleIncomingConnectionRequest), _myListener)
        End Sub
有帮助吗?

解决方案

这要看你的LocalIP。你可以将其设置为“0.0.0.0”监听所有接口,也可以将其设置为一个特定的接口进行监听。

在你的代码,你将它设置为您的主机名的第一个IP地址。这可能并不总是工作,虽然。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top