@@ -71,6 +71,11 @@ public class HttpListener
7171 /// </summary>
7272 private int m_Port ;
7373
74+ /// <summary>
75+ /// the local endpoint to bind the socket to. if Null the default is used
76+ /// </summary>
77+ private IPAddress m_localEndpointIP ;
78+
7479 /// <summary>
7580 /// Indicates whether the listener is started and is currently accepting
7681 /// connections.
@@ -136,13 +141,15 @@ public HttpListener(string prefix)
136141 /// <param name="port">The port to start listening on. If -1, the
137142 /// default port is used (port 80 for http, or port 443 for https).
138143 /// </param>
144+ /// <param name="localEndpointIP"> The local endpoint to bind the socket to. If Null the default is used
145+ /// </param>
139146 /// <remarks>In the desktop version of .NET, the constructor for this
140147 /// class has no arguments.</remarks>
141- public HttpListener ( string prefix , int port )
148+ public HttpListener ( string prefix , int port , IPAddress localEndpointIP = null )
142149 {
143150 lockObj = new object ( ) ;
144151
145- InitListener ( prefix , port ) ;
152+ InitListener ( prefix , port , localEndpointIP ) ;
146153 }
147154
148155 /// <summary>
@@ -153,7 +160,7 @@ public HttpListener(string prefix, int port)
153160 /// <param name="port">The port to start listening on. If -1, the
154161 /// default port is used (port 80 for http, or port 443 for https).
155162 /// </param>
156- private void InitListener ( string prefix , int port )
163+ private void InitListener ( string prefix , int port , IPAddress localEndpointIp = null )
157164 {
158165 switch ( prefix . ToLower ( ) )
159166 {
@@ -179,6 +186,10 @@ private void InitListener(string prefix, int port)
179186 m_Port = port ;
180187 }
181188
189+ if ( localEndpointIp != null )
190+ {
191+ m_localEndpointIP = localEndpointIp ;
192+ }
182193 // Default members initialization
183194 m_maxResponseHeadersLen = 4 ;
184195 m_RequestArrived = new AutoResetEvent ( false ) ;
@@ -494,7 +505,7 @@ public void Start()
494505 // empty on purpose
495506 }
496507
497- IPAddress addr = IPAddress . GetDefaultLocalAddress ( ) ;
508+ IPAddress addr = m_localEndpointIP ?? IPAddress . GetDefaultLocalAddress ( ) ;
498509
499510 IPEndPoint endPoint = new IPEndPoint ( addr , m_Port ) ;
500511 m_listener . Bind ( endPoint ) ;
0 commit comments