diff --git a/AsyncTcpServer.cs b/AsyncTcpServer.cs index 552c12f..35e8468 100644 --- a/AsyncTcpServer.cs +++ b/AsyncTcpServer.cs @@ -73,11 +73,13 @@ namespace ForwardingServer } else { - Personm deserializedPerson = JsonSerializer.Deserialize(DAT)!; - DATA.MachineTable.Rows.Add(client.Id, client.IP, 30, "/" + deserializedPerson.SERVEREnterprise, - deserializedPerson.SERVERMachineName, deserializedPerson.MachineVer); - Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + " [LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT); - + if (IsValidJson(DAT)) + { + Personm deserializedPerson = JsonSerializer.Deserialize(DAT)!; + DATA.MachineTable.Rows.Add(client.Id, client.IP, 30, "/" + deserializedPerson.SERVEREnterprise, + deserializedPerson.SERVERMachineName, deserializedPerson.MachineVer); + Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-DD HH:mm:ss:fff") + " [LINK ID " + client.Id + " IP " + client.IP + "]\nDATA =" + DAT); + } } } } @@ -107,7 +109,17 @@ namespace ForwardingServer await service.StartAsync();//启动 Console.WriteLine("TcpServer:Run"); - } - + } + + // 辅助方法检查JSON格式 + private static bool IsValidJson(string jsonString) + { + if (string.IsNullOrEmpty(jsonString)) + return false; + + jsonString = jsonString.Trim(); + return (jsonString.StartsWith("{") && jsonString.EndsWith("}")) || + (jsonString.StartsWith("[") && jsonString.EndsWith("]")); + } } }