@@ -71,6 +71,11 @@ public class HttpListener
71
71
/// </summary>
72
72
private int m_Port ;
73
73
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
+
74
79
/// <summary>
75
80
/// Indicates whether the listener is started and is currently accepting
76
81
/// connections.
@@ -136,13 +141,15 @@ public HttpListener(string prefix)
136
141
/// <param name="port">The port to start listening on. If -1, the
137
142
/// default port is used (port 80 for http, or port 443 for https).
138
143
/// </param>
144
+ /// <param name="localEndpointIP"> The local endpoint to bind the socket to. If Null the default is used
145
+ /// </param>
139
146
/// <remarks>In the desktop version of .NET, the constructor for this
140
147
/// class has no arguments.</remarks>
141
- public HttpListener ( string prefix , int port )
148
+ public HttpListener ( string prefix , int port , IPAddress localEndpointIP = null )
142
149
{
143
150
lockObj = new object ( ) ;
144
151
145
- InitListener ( prefix , port ) ;
152
+ InitListener ( prefix , port , localEndpointIP ) ;
146
153
}
147
154
148
155
/// <summary>
@@ -153,7 +160,7 @@ public HttpListener(string prefix, int port)
153
160
/// <param name="port">The port to start listening on. If -1, the
154
161
/// default port is used (port 80 for http, or port 443 for https).
155
162
/// </param>
156
- private void InitListener ( string prefix , int port )
163
+ private void InitListener ( string prefix , int port , IPAddress localEndpointIp = null )
157
164
{
158
165
switch ( prefix . ToLower ( ) )
159
166
{
@@ -179,6 +186,10 @@ private void InitListener(string prefix, int port)
179
186
m_Port = port ;
180
187
}
181
188
189
+ if ( localEndpointIp != null )
190
+ {
191
+ m_localEndpointIP = localEndpointIp ;
192
+ }
182
193
// Default members initialization
183
194
m_maxResponseHeadersLen = 4 ;
184
195
m_RequestArrived = new AutoResetEvent ( false ) ;
@@ -494,7 +505,7 @@ public void Start()
494
505
// empty on purpose
495
506
}
496
507
497
- IPAddress addr = IPAddress . GetDefaultLocalAddress ( ) ;
508
+ IPAddress addr = m_localEndpointIP ?? IPAddress . GetDefaultLocalAddress ( ) ;
498
509
499
510
IPEndPoint endPoint = new IPEndPoint ( addr , m_Port ) ;
500
511
m_listener . Bind ( endPoint ) ;
0 commit comments