You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SocketUtil.cs 1.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace Shadowsocks.Util.Sockets
  5. {
  6. public static class SocketUtil
  7. {
  8. private class DnsEndPoint2 : DnsEndPoint
  9. {
  10. public DnsEndPoint2(string host, int port) : base(host, port)
  11. {
  12. }
  13. public DnsEndPoint2(string host, int port, AddressFamily addressFamily) : base(host, port, addressFamily)
  14. {
  15. }
  16. public override string ToString()
  17. {
  18. return this.Host + ":" + this.Port;
  19. }
  20. }
  21. public static EndPoint GetEndPoint(string host, int port)
  22. {
  23. IPAddress ipAddress;
  24. bool parsed = IPAddress.TryParse(host, out ipAddress);
  25. if (parsed)
  26. {
  27. return new IPEndPoint(ipAddress, port);
  28. }
  29. // maybe is a domain name
  30. return new DnsEndPoint2(host, port);
  31. }
  32. public static void FullClose(this System.Net.Sockets.Socket s)
  33. {
  34. try
  35. {
  36. s.Shutdown(SocketShutdown.Both);
  37. }
  38. catch (Exception)
  39. {
  40. }
  41. try
  42. {
  43. s.Disconnect(false);
  44. }
  45. catch (Exception)
  46. {
  47. }
  48. try
  49. {
  50. s.Close();
  51. }
  52. catch (Exception)
  53. {
  54. }
  55. try
  56. {
  57. s.Dispose();
  58. }
  59. catch (Exception)
  60. {
  61. }
  62. }
  63. }
  64. }