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