新增了端口占用检测

This commit is contained in:
WenX
2015-02-14 00:14:55 +08:00
parent 138e4f0715
commit 336e0368a1
+20
View File
@@ -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.