forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndPointExtensions.cs
63 lines (58 loc) · 1.83 KB
/
EndPointExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections.Generic;
using System.Net;
namespace System
{
/// <summary>网络结点扩展</summary>
public static class EndPointExtensions
{
/// <summary>
///
/// </summary>
/// <param name="endpoint"></param>
/// <returns></returns>
public static String ToAddress(this EndPoint endpoint)
{
return ((IPEndPoint)endpoint).ToAddress();
}
/// <summary>
///
/// </summary>
/// <param name="endpoint"></param>
/// <returns></returns>
public static String ToAddress(this IPEndPoint endpoint)
{
return String.Format("{0}:{1}", endpoint.Address, endpoint.Port);
}
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static IPEndPoint ToEndPoint(this String address)
{
var array = address.Split(new String[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (array.Length != 2)
{
throw new Exception("Invalid endpoint address: " + address);
}
var ip = IPAddress.Parse(array[0]);
var port = Int32.Parse(array[1]);
return new IPEndPoint(ip, port);
}
/// <summary>
///
/// </summary>
/// <param name="addresses"></param>
/// <returns></returns>
public static IEnumerable<IPEndPoint> ToEndPoints(this String addresses)
{
var array = addresses.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var list = new List<IPEndPoint>();
foreach (var item in array)
{
list.Add(item.ToEndPoint());
}
return list;
}
}
}