You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
988 B
40 lines
988 B
8 months ago
|
using System;
|
||
|
|
||
|
namespace EGFramework {
|
||
|
public static class EGIpExtension
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Get host from IP. Such as 192.168.0.1:5555 => get 192.168.0.1
|
||
|
/// </summary>
|
||
|
/// <param name="self"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string GetHostByIp(this string ip)
|
||
|
{
|
||
|
int colonIndex = ip.IndexOf(":");
|
||
|
string host = "";
|
||
|
if (colonIndex != -1)
|
||
|
{
|
||
|
host = ip.Substring(0, colonIndex);
|
||
|
}
|
||
|
return host;
|
||
|
}
|
||
|
|
||
|
public static int GetPortByIp(this string ip)
|
||
|
{
|
||
|
int colonIndex = ip.IndexOf(":");
|
||
|
string portString = ip.Substring(colonIndex + 1);
|
||
|
int port;
|
||
|
if (int.TryParse(portString, out port))
|
||
|
{
|
||
|
//nothing to do
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
port = 0;
|
||
|
}
|
||
|
return port;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|