wasSharpNET – Diff between revs 9 and 11
?pathlinks?
Rev 9 | Rev 11 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System.Net; |
7 | using System.Net; |
|
8 | using System.Net.Sockets; |
8 | using System.Net.Sockets; |
|
9 | |
9 | |
|
10 | namespace wasSharpNET.Network.TCP |
10 | namespace wasSharpNET.Network.TCP |
|
11 | { |
11 | { |
|
12 | public static class Utilities |
12 | public static class Utilities |
|
13 | { |
13 | { |
|
14 | /////////////////////////////////////////////////////////////////////////// |
14 | /////////////////////////////////////////////////////////////////////////// |
|
15 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
15 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
16 | /////////////////////////////////////////////////////////////////////////// |
16 | /////////////////////////////////////////////////////////////////////////// |
|
17 | /// <summary> |
17 | /// <summary> |
|
18 | /// Try to find an unused port. |
18 | /// Try to find an unused port. |
|
19 | /// </summary> |
19 | /// </summary> |
|
20 | /// <param name="address">the address associated with the port</param> |
20 | /// <param name="address">the address associated with the port</param> |
|
21 | /// <param name="port">an integer to hold the port if found</param> |
21 | /// <param name="port">an integer to hold the port if found</param> |
|
22 | /// <returns>true if an unused port could be found</returns> |
22 | /// <returns>true if an unused port could be found</returns> |
|
23 | public static bool TryGetUnusedPort(IPAddress address, out int port) |
23 | public static bool TryGetUnusedPort(IPAddress address, out int port) |
|
24 | { |
24 | { |
|
25 | try |
25 | try |
|
26 | { |
26 | { |
|
27 | var l = new TcpListener(address, 0); |
27 | var l = new TcpListener(address, 0); |
|
28 | l.Start(); |
28 | l.Start(); |
|
29 | port = ((IPEndPoint) l.LocalEndpoint).Port; |
29 | port = ((IPEndPoint)l.LocalEndpoint).Port; |
|
30 | l.Stop(); |
30 | l.Stop(); |
|
31 | return true; |
31 | return true; |
|
32 | } |
32 | } |
|
33 | catch |
33 | catch |
|
34 | { |
34 | { |
|
35 | port = default(int); |
35 | port = default(int); |
|
36 | return false; |
36 | return false; |
|
37 | } |
37 | } |
|
38 | } |
38 | } |
|
39 | } |
39 | } |
|
40 | } |
- | ||
41 | |
40 | } |
|
- | 41 | |
||
42 |
|
- | ||
43 | |
- | ||
44 | |
- | ||
45 | |
- |