Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
b168f3fa
Commit
b168f3fa
authored
Jul 26, 2023
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加 扩展 Misc.StringConverter.ToIPEndPoint 支持转换异常时,使用默认值.
parent
8593d013
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
4 deletions
+75
-4
StringConverter.cs
Project.FLY.Misc/MISC/StringConverter.cs
+75
-4
No files found.
Project.FLY.Misc/MISC/StringConverter.cs
View file @
b168f3fa
...
@@ -81,7 +81,7 @@ namespace Misc
...
@@ -81,7 +81,7 @@ namespace Misc
return
null
;
return
null
;
string
[]
s
=
hostNameOrAddress
.
Split
(
new
char
[]
{
':'
},
StringSplitOptions
.
RemoveEmptyEntries
);
string
[]
s
=
hostNameOrAddress
.
Split
(
new
char
[]
{
':'
},
StringSplitOptions
.
RemoveEmptyEntries
);
if
(
s
.
Length
==
1
)
if
(
s
.
Length
==
1
)
{
{
IPAddress
ip
;
IPAddress
ip
;
int
port
=
20006
;
int
port
=
20006
;
...
@@ -125,14 +125,16 @@ namespace Misc
...
@@ -125,14 +125,16 @@ namespace Misc
}
}
catch
catch
{
{
try
{
try
{
IPAddress
[]
ips
=
System
.
Net
.
Dns
.
GetHostEntry
(
s
[
0
]).
AddressList
;
IPAddress
[]
ips
=
System
.
Net
.
Dns
.
GetHostEntry
(
s
[
0
]).
AddressList
;
if
(
ips
.
Length
==
1
)
if
(
ips
.
Length
==
1
)
ip
=
ips
[
0
];
ip
=
ips
[
0
];
else
else
ip
=
IPAddress
.
Parse
(
"127.0.0.1"
);
ip
=
IPAddress
.
Parse
(
"127.0.0.1"
);
}
}
catch
{
catch
{
return
null
;
return
null
;
}
}
}
}
...
@@ -141,7 +143,76 @@ namespace Misc
...
@@ -141,7 +143,76 @@ namespace Misc
return
null
;
return
null
;
}
}
public
static
string
IPEndPoint2DirectoryName
(
IPEndPoint
ipep
)
// 摘要:
// 将主机名或 IP 地址解析为 IPEndPoint 实例。
public
static
IPEndPoint
ToIPEndPoint
(
string
hostNameOrAddress
,
IPAddress
default_address
,
int
default_port
)
{
IPAddress
ip
=
default_address
;
int
port
=
default_port
;
if
(
string
.
IsNullOrEmpty
(
hostNameOrAddress
))
{
return
new
IPEndPoint
(
ip
,
port
);
}
string
[]
s
=
hostNameOrAddress
.
Split
(
new
char
[]
{
':'
},
StringSplitOptions
.
RemoveEmptyEntries
);
if
(
s
.
Length
==
1
)
{
try
{
ip
=
IPAddress
.
Parse
(
s
[
0
]);
}
catch
{
try
{
IPAddress
[]
ips
=
System
.
Net
.
Dns
.
GetHostEntry
(
s
[
0
]).
AddressList
;
if
(
ips
.
Length
==
1
)
ip
=
ips
[
0
];
}
catch
{
}
}
}
else
if
(
s
.
Length
==
2
)
{
try
{
port
=
int
.
Parse
(
s
[
1
]);
}
catch
{
}
try
{
ip
=
IPAddress
.
Parse
(
s
[
0
]);
}
catch
{
try
{
IPAddress
[]
ips
=
System
.
Net
.
Dns
.
GetHostEntry
(
s
[
0
]).
AddressList
;
if
(
ips
.
Length
==
1
)
ip
=
ips
[
0
];
}
catch
{
}
}
}
return
new
IPEndPoint
(
ip
,
port
);
}
public
static
string
IPEndPoint2DirectoryName
(
IPEndPoint
ipep
)
{
{
return
ipep
.
Address
.
ToString
()
+
"_"
+
ipep
.
Port
.
ToString
();
return
ipep
.
Address
.
ToString
()
+
"_"
+
ipep
.
Port
.
ToString
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment