开发者

Bind TCP server to specific adaptor

开发者 https://www.devze.com 2022-12-15 09:55 出处:网络
How can I specify which adaptor to begin listening on? I have an application running on a PC which happens to have 2 network adaptors running on different subnets (one network for Business LAN infras

How can I specify which adaptor to begin listening on?

I have an application running on a PC which happens to have 2 network adaptors running on different subnets (one network for Business LAN infrastructure, one for TCP cameras)

I have a class which opens up a TCP server and listens on a specific port for connections coming in from clients on the LAN.

The problem is that my TCP server class initialises and begins listening on the adaptor which is connected to the cameras. Any connection request coming in from the Business LAN fails, it is not dealt with.

The question is, How can I specify which adaptor to begin listening on?

Code excerpt below (this is not the full class, just key methods)

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
    开发者_StackOverflow中文版        _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


It depends on your LocalIP. You could set it to "0.0.0.0" to listen on all interfaces, or you can set it to listen on a specific interface.

In your code, you set it to the first IP address of your hostname. Which may not always work though.

0

精彩评论

暂无评论...
验证码 换一张
取 消