From 336e0368a119360f7d7dc71daf5c758100ffd082 Mon Sep 17 00:00:00 2001 From: WenX Date: Sat, 14 Feb 2015 00:14:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E5=8D=A0=E7=94=A8=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shadowsocks-csharp/Controller/Listener.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/shadowsocks-csharp/Controller/Listener.cs b/shadowsocks-csharp/Controller/Listener.cs index 923bc0e7..9943e536 100755 --- a/shadowsocks-csharp/Controller/Listener.cs +++ b/shadowsocks-csharp/Controller/Listener.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; @@ -24,10 +25,29 @@ namespace Shadowsocks.Controller this._services = services; } + private bool CheckIfPortInUse(int port) + { + IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); + + foreach (IPEndPoint endPoint in ipEndPoints) + { + if (endPoint.Port == port) + { + return true; + } + } + return false; + } + public void Start(Configuration config) { this._config = config; this._shareOverLAN = config.shareOverLan; + + if (CheckIfPortInUse(_config.localPort)) + throw new Exception(I18N.GetString("Port already in use")); + try { // Create a TCP/IP socket.