Guest User

Untitled

a guest
Jun 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. public static bool TryPortNumber(int port)
  2. {
  3. try
  4. {
  5.  
  6. using (var client = new System.Net.Sockets.TcpClient(new System.Net.IPEndPoint(System.Net.IPAddress.Any, port)))
  7. {
  8. return true;
  9. }
  10. }
  11. catch (System.Net.Sockets.SocketException error)
  12. {
  13. if (error.SocketErrorCode == System.Net.Sockets.SocketError.AddressAlreadyInUse /* check this is the one you get */ )
  14. return false;
  15. /* unexpected error that we DON'T have handling for here */
  16. throw error;
  17. }
  18. }
Add Comment
Please, Sign In to add comment