Browse Source

新增了端口占用检测

tags/2.3.1
WenX 9 years ago
parent
commit
336e0368a1
1 changed files with 20 additions and 0 deletions
  1. +20
    -0
      shadowsocks-csharp/Controller/Listener.cs

+ 20
- 0
shadowsocks-csharp/Controller/Listener.cs 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.


Loading…
Cancel
Save