Skip to content

Commit 9ec59db

Browse files
itldgchenxuuu
authored andcommitted
fix: 修复端口被占用后本地TCP服务器无法再打开的问题 #171
1 parent 79f610f commit 9ec59db

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

llcom/Pages/TcpLocalPage.xaml.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,17 @@ private bool StartServer(string ip, int port)
187187
if (Server != null)
188188
return false;
189189
IPAddress localAddr = IPAddress.Parse(ip);
190-
Server = new TcpListener(localAddr, port);
191-
Server.Start();
190+
try
191+
{
192+
Server = new TcpListener(localAddr, port);
193+
Server.Start();
194+
}
195+
catch (Exception ex)
196+
{
197+
Server = null;
198+
throw ex;
199+
}
200+
192201
var isV6 = ip.Contains(":");
193202
ShowData($"🛰 {(isV6 ? "[" : "")}{ip}{(isV6 ? "]" : "")}:{port}");
194203
AsyncCallback newConnectionCb = null;

0 commit comments

Comments
 (0)