{"record":{"id":"2898484cdc56538e","repo":"netchx/netch","slug":"the-0-port-is-in-use","errorCode":null,"errorMessage":"The {0} port is in use.","messagePattern":"The (.+?) port is in use\\.","errorType":"exception","errorClass":"MessageException","httpStatus":null,"severity":"error","filePath":"Netch/Controllers/MainController.cs","lineNumber":142,"sourceCode":"        }\n        catch (Exception e)\n        {\n            Log.Error(e, \"MainController Stop Error\");\n        }\n\n        ServerController = null;\n        ModeController = null;\n    }\n\n    public static void PortCheck(ushort port, string portName, PortType portType = PortType.Both)\n    {\n        try\n        {\n            PortHelper.CheckPort(port, portType);\n        }\n        catch (PortInUseException)\n        {\n            throw new MessageException(i18N.TranslateFormat(\"The {0} port is in use.\", $\"{portName} ({port})\"));\n        }\n        catch (PortReservedException)\n        {\n            throw new MessageException(i18N.TranslateFormat(\"The {0} port is reserved by system.\", $\"{portName} ({port})\"));\n        }\n    }\n\n    public static void TryReleaseTcpPort(ushort port, string portName)\n    {\n        foreach (var p in PortHelper.GetProcessByUsedTcpPort(port))\n        {\n            var fileName = p.MainModule?.FileName;\n            if (fileName == null)\n                continue;\n\n            if (fileName.StartsWith(Global.NetchDir))\n            {\n                p.Kill();","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/netchx/netch/blob/9d99eb1c5a2acbf2a34f2600f94242601019a300/Netch/Controllers/MainController.cs#L124-L160","documentation":"Thrown by MainController.PortCheck when PortHelper.CheckPort raises PortInUseException — an active TCP or UDP listener already exists on the requested port (checked via IPGlobalProperties.GetActiveTcpListeners/GetActiveUdpListeners). The formatted message includes the human-readable portName and the port number.","triggerScenarios":"Another application (or a previous, not-fully-stopped Netch process) is bound to the Socks5 local port, the mode controller's port, or the AioDNS port; calling PortCheck for a port a non-Netch app holds.","commonSituations":"Netch crashed without releasing its Socks5 port and is restarted; another proxy client (Clash, v2rayN) uses the same port; a development server occupies the port.","solutions":["Identify the holder: `netstat -ano | findstr :<port>` then tasklist / find the PID.","Stop the conflicting application or pick a different port in Settings.","Use MainController.TryReleaseTcpPort if the holder is another Netch process (it auto-kills Netch-owned processes).","Restart Netch cleanly so stale listeners are released."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"MainController.PortCheck(port, portName, PortType.Both);\n// throws MessageException(\"The {portName} (port) port is in use.\") if a listener exists","typeGuard":null,"tryCatchPattern":"try { MainController.PortCheck(port, portName); }\ncatch (MessageException ex) when (ex.Message.Contains(\"port is in use\"))\n{\n    // prompt the user to change the port or stop the conflicting app\n    SuggestAlternatePort(port);\n}","preventionTips":["Call MainController.PortCheck during settings save, not only at Start, to surface conflicts early.","Default Netch to low, rarely-used ports (1080, 1086) to avoid common conflicts.","Use TryReleaseTcpPort when the holder might be another Netch process.","Teach users to check `netstat -ano | findstr :<port>`."],"tags":["port","network","config","validation"],"backgroundTag":null,"analyzedSha":"9d99eb1c5a2acbf2a34f2600f94242601019a300","analyzedAt":"2026-08-13T14:12:19.105Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}