TCP 서버를 특정 어댑터에 바인딩하십시오
-
19-09-2019 - |
문제
청취를 시작할 어댑터를 어떻게 지정할 수 있습니까?
PC에서 다른 서브넷에서 2 개의 네트워크 어댑터가 실행되는 응용 프로그램이 있습니다 (비즈니스 LAN 인프라를위한 네트워크, TCP 카메라 용 1 개).
TCP 서버를 열고 LAN의 클라이언트에서 들어오는 연결을 위해 특정 포트에서 리스닝하는 클래스가 있습니다.
문제는 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 주소로 설정합니다. 그래도 항상 작동하지는 않습니다.
제휴하지 않습니다 StackOverflow