diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs
index aac9a10..9ec6466 100644
--- a/Api/Utility/General.cs
+++ b/Api/Utility/General.cs
@@ -1,4 +1,5 @@
using System.Collections;
+using System.Net;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
@@ -56,30 +57,42 @@ namespace Milimoe.FunGame.Core.Api.Utility
}
///
- /// 判断字符串是否是一个FunGame可接受的服务器地址
+ /// 判断字符串是否是一个FunGame可接受的服务器地址
+ /// 此方法可以解析域名
///
///
- ///
- public static ErrorIPAddressType IsServerAddress(string str)
+ ///
+ ///
+ /// 返回地址验证结果,同时输出服务器地址和端口号
+ public static ErrorIPAddressType IsServerAddress(string str, out string addr, out int port)
{
- string[] strs = str.Split(':');
+ addr = "";
+ port = 22222;
string ip;
- int port;
- if (strs.Length < 2)
+ // 包含端口号时,需要先截取
+ string[] strs = str.Split(':');
+ if (strs.Length == 1)
+ {
+ addr = str;
+ }
+ else if (strs.Length > 1)
+ {
+ addr = strs[0];
+ port = int.Parse(strs[1]);
+ }
+ else if (strs.Length > 2)
+ {
+ return ErrorIPAddressType.WrongFormat;
+ }
+ try
+ {
+ ip = GetIPAddress(addr);
+ }
+ catch
{
ip = strs[0];
- port = 22222;
}
- else if (strs.Length < 3)
- {
- ip = strs[0];
- port = Convert.ToInt32(strs[1]);
- }
- else return ErrorIPAddressType.WrongFormat;
- if (IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.None;
- else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.IsNotIP;
- else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorIPAddressType.IsNotPort;
- else return ErrorIPAddressType.WrongFormat;
+ return IsServerAddress(ip, port);
}
///
@@ -90,9 +103,21 @@ namespace Milimoe.FunGame.Core.Api.Utility
///
public static ErrorIPAddressType IsServerAddress(string ip, int port)
{
- if (IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.None;
- else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.IsNotIP;
- else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorIPAddressType.IsNotPort;
+ if (IsIP(ip))
+ {
+ if (port > 0 && port < 65536)
+ {
+ return ErrorIPAddressType.None;
+ }
+ else
+ {
+ return ErrorIPAddressType.IsNotPort;
+ }
+ }
+ else if (port > 0 && port < 65536)
+ {
+ return ErrorIPAddressType.IsNotAddress;
+ }
else return ErrorIPAddressType.WrongFormat;
}
@@ -119,6 +144,20 @@ namespace Milimoe.FunGame.Core.Api.Utility
return -1;
}
+ ///
+ /// 解析域名为IP地址
+ ///
+ ///
+ ///
+ ///
+ internal static string GetIPAddress(string domain, System.Net.Sockets.AddressFamily family = System.Net.Sockets.AddressFamily.InterNetwork)
+ {
+ // 如果是域名,则解析为IP地址
+ IPHostEntry entrys = Dns.GetHostEntry(domain);
+ // 这里使用断言,请自行添加try catch配合使用
+ return entrys.AddressList.Where(addr => addr.AddressFamily == family).FirstOrDefault()!.ToString();
+ }
+
///
/// 返回目标对象的Json字符串
///
diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs
index 4a65186..4ce979a 100644
--- a/Controller/RunTimeController.cs
+++ b/Controller/RunTimeController.cs
@@ -81,13 +81,13 @@ namespace Milimoe.FunGame.Core.Controller
///
/// 连接服务器
///
- ///
+ ///
///
///
- public ConnectResult Connect(string ip, int port)
+ public ConnectResult Connect(string addr, int port)
{
ArrayList ConnectArgs = [];
- if (!BeforeConnect(ref ip, ref port, ConnectArgs))
+ if (!BeforeConnect(ref addr, ref port, ConnectArgs))
{
return ConnectResult.ConnectFailed;
}
@@ -98,7 +98,7 @@ namespace Milimoe.FunGame.Core.Controller
string notice = "";
// 检查服务器IP地址和端口是否正确
- if (ip == "" || port <= 0)
+ if (addr == "" || port <= 0)
{
result = ConnectResult.FindServerFailed;
}
@@ -106,7 +106,7 @@ namespace Milimoe.FunGame.Core.Controller
{
// 与服务器建立连接
_Socket?.Close();
- _Socket = Socket.Connect(ip, port);
+ _Socket = Socket.Connect(addr, port);
if (_Socket != null && _Socket.Connected)
{
if (_Socket.Send(SocketMessageType.Connect, ConnectArgs.Cast